Basics
- There are no String methods to change an existing String. There are many methods to produce new Strings.
- 'x' and "x" are different. The former is a char; the latter is a String.
- null and "" are different. The former indicates a nonreference; the latter is a String with length 0.
- A character string within double quotes is a String literal; e.g., "starting".
- "name" and name are different. The former is a String literal; the latter is an identifier.
- The characters in a String are accessible by their index. The first character has index 0, the second character has index 1, and so on.
Important methods
- Suppose s is a String, t is a String, u is a String, m is an int, and n is an int.
- s.length() is the number of characters in the character string represented by s.
- s.charAt( m ) is a the char value of the character in s at index m.
- s.toLowerCase() is a new string whose characters are the lowercase equivalents of the character string represented by s.
- s.toUpperCase() is a new string whose characters are the uppercase equivalents of the character string represented by s.
- s.indexOf( t, m ) is the int value equal to the index of the first occurrence of t's character string within s's character string when starting the search at index m. If there is no occurrence then the value of the method is -1.
- s.lastIndexOf( t, m ) is the int value equal to the index of the last occurrence of t's character string within s's character string that starts on or before index m. If there is no occurrence then the value of the method is -1.
- s.substring( m, n ) is a new string representing the substring of s starting with its character at index m and continuing to the character with index n-1.
- s.replace( t, u ) is a new string formed by first copying all of the characters of s and then replacing its first occurrence of substring t with substring u.
- s.replaceAll( t, u ) is a new string formed by first copying all of the characters of s and then replacing all occurrences of substring t with substring u.
Convience methods
- s.substring( m ) is a new string representing the substring of s starting with its character at index m and continuing to the end of s.
- s.indexOf( t ) is equivalent to s.indexOf( t, 0 ).
- s.lastIndexOf( t ) is equivalent to s.lastIndexOf( t, m ), where m+1 is the length of character string represented by s.