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.
Declaration
- Subsequent sections of this document are explaining how some of the many String message methods work. In that discussion assume
- s, t, and u are strings,
- m and n are integers.
Important methods
- 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.trim() returns a new copy of character string s with any leading or trailing whitespace removed.
- s.toLowerCase() returns a new copy of the character string s with any uppercase characters replaced by their lowercase counterparts.
- s.toUpperCase() returns a new copy of the character string s with any lowerase characters replaced by their uppercase counterparts.
- s.indexOf( t, m) returns 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 ) returns 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 ) returns 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.contains( t ) returns whether s constains string t as a substring.
- s.startsWith( t ) returns whether s starts with string t.
- s.replace( t, u ) returns a new string formed by first copying all of the characters of s and then replacing all occurrences of substring t with string u.
- s.replaceFirst( t, u ) returns a new string formed by first copying all of the characters of s and then replacing its first occurrence of the string pattern indicated by t with string u.
- s.replaceAll( t, u ) returns a new string formed by first copying all of the characters of s and then replacing all occurrences of the string pattern indicated by t with string u.
- s.compareTo( t ) returns an integer value indicating the lexicographical (alphabetical) relationship between s and t:
- Returns 0 when s's character sequence is the same as t's character sequence;
- Returns a negative value when s's character sequence occurs lexicographically before t's character sequence;
- Returns a positive value when s's character sequence occurs lexicographically after t's character sequence;
- s.equals( t ) returns a boolean value indicating whether s and t represent the same character sequence (i.e., do they look alike).
Convenience methods
- s.substring( m ) returns 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.