Page 202 - Introduction to Programming with Java: A Problem Solving Approach
P. 202
168 Chapter 5 Using Pre-Built Methods
public String compareTo(String str)
Returns an integer that indicates the lexicographical ordering of the calling string when compared to the argument string. If the calling string is “greater than” the argument string, a positive number is returned. If the calling string is “less than” the argument string, a negative number is returned. If the calling string equals the argument string, zero is returned.
public int indexOf(int ch)
Returns the position of the first occurrence of the specified character.
public int indexOf(int ch, int fromIndex)
Returns the position of the first occurrence of the specified character at or after fromIndex. public int indexOf(String str)
Returns the start position of the first occurrence of the specified string.
public int indexOf(String str, int fromIndex)
Returns the start position of the first occurrence of the specified string at or after fromIndex. public boolean isEmpty()
Returns true if the calling string is the empty string (""). Otherwise, returns false. public String replaceAll(String target, String replacement)
Returns a new string with all occurrences of the calling string’s target replaced by replacement.
Apago PDF Enhancer
public String replaceFirst(String target, String replacement)
Returns a new string with the first occurrence of the calling string’s target replaced by replacement.
public String substring(int beginIndex)
Returns the portion of the calling string from beginIndex to the end.
public String substring(int beginIndex, int afterEndIndex)
Returns the portion of the calling string from beginIndex to just before afterEndIndex. public String toLowerCase()
Returns a new string with all characters in the calling string converted to lowercase.
public String toUpperCase()
Returns a new string with all characters in the calling string converted to uppercase.
public String trim()
Returns a new string with all whitespace removed from the start and end of the calling string.
Figure 5.8 API headings and brief descriptions of some of the methods in the String class
it contains the empty string. For example, when reading an input string from a user, you might want to check for the empty string as part of input validation. The following code fragment illustrates:
if (userInput.equals(""))
...