Notes On JAVA 10.2005.6: The String Data Type: Constructor Input
Notes On JAVA 10.2005.6: The String Data Type: Constructor Input
Output Example:
System.out.println (s); prints string s to screen. System.out.println ("Hello World"); will print Hello World to the screen. int len = s.length (); returns an integer
gives an integer representing the number of characters in the string. Example: s = "Hello World"; int len = s.length (); will return 11. The indexOf method int pos = s.indexOf (, ); returns an integer
searches for the first occurrence of the comma-space combination in the mainstring and returns the position of the first character of the first occurrence of the substring in the mainstring (an integer). If not found, the function returns 0. Example: s = "Size doesnt matter"; int pos = s.indexOf ("do"); will return a 5. The substring method s = s.subString (start, end); returns a string
copies a substring from the mainstring, starting at start (inclusive) to end (exclusive). If start is greater than end, an empty string will be returned. Example: s = "This is copied"; String ss = s.subString (8, 14); will assign the value copied to ss. The concat method s = s.concat (ss); returns a string
combines the strings s and ss, the latter being added to the back of s. It concatenates ss to the end of s. Example: s = "blah"; s = s.concat ("blah"); will assign the value blahblah to s. Note: This can also be written as: s = s + "blah"; OR (different order) s = "blah" + s; OR s = "blah" + "blah" + "blah"; OR s = s + ss; etc. The equals method boolean e = s.equals (ss); returns a boolean
indicates whether the strings s and ss are absolutely identical. Example: if (s.equals ("yes")) after s was entered from keyboard will return true (i.e. let the program proceed) if and only if yes was entered. The equalsIgnoreCase method boolean e = s.equalsIgnoreCase (ss); returns a boolean
indicates whether the strings s and ss are identical in spelling but not necessarily in case. Example: if (s.equalsIgnoreCase ("yes")) after s was entered from keyboard will return true for yes, Yes or YES.
returns the character at a specific position from a string (NB: the int may NOT be bigger than the index of the last letter!) so that the characters can be accessed individually by means of the index, e.g. to print them backwards, one below the other, etc. Example: s = "Size doesnt matter"; char first = s.charAt (0); will return S, and char x = s.charAt (9); will return n. The toUpperCase method String uc = s.toUpperCase (); returns a String
returns a string where all characters are in upper case. Example: s = "Hello World"; uc = s.toUpperCase (); will return "HELLO WORLD". The toLowerCase method String lc = s.toLowerCase (); returns a String
returns a string where all characters are in lower case. Example: s = "Hello World"; lc = s.toLowerCase (); will return "hello world".
EXERCISES:
1. Write a program that will read in your name (a String) and then print the name getting shorter and shorter. getting longer and longer, e.g. P Pe Pet Pete Peter 2. Write a program that will read in your name (a String) and then print it backwards. downwards. as a square, e.g. OR (more advanced) as a diamond, e.g. P e t e r e e t t e e r e t e P J o o h n h o o J 3. Write a program that will read in your name (a String), convert all letters to lower case, sort the letters and print it, e.g. Peter will become eeprt 4. Write a program that will read in a sentence (a String) and then print each word on a new line, e.g. This is a sentence h h n
_________________________________________________
mb