Searching for Characters and Substrings in Strings
The following overloaded methods can be used to find the index of a character or the start index of a substring in a string. These methods search forward toward the end of the string. In other words, the index of the first occurrence of the character or substring is found. If the search is unsuccessful, the value -1 is returned.
int indexOf(int ch)
int indexOf(int ch, int fromIndex)
The first method finds the index of the first occurrence of the argument character in a string. The second method finds the index of the first occurrence of the argument character in a string, starting at the index specified in the second argument. If the index argument is negative, the index is assumed to be 0. If the index argument is greater than the length of the string, it is effectively considered to be equal to the length of the string, resulting in the value -1 being returned.
int indexOf(String str)
int indexOf(String str, int fromIndex)
The first method finds the start index of the first occurrence of the substring argument in a string. The second method finds the start index of the first occurrence of the substring argument in a string, starting at the index specified in the second argument.
The String class also defines a set of methods that search for a character or a substring, but the search is backward toward the start of the string. In other words, the index of the last occurrence of the character or substring is found.
int lastIndexOf(int ch)
int lastIndexOf(int ch, int fromIndex)
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex)
The following methods can be used to create a string in which all occurrences of a character or a subsequence in a string have been replaced with another character or subsequence:
String replace(char oldChar, char newChar)
String replace(CharSequence target, CharSequence replacement)
The first method returns a new String object that is the result of replacing all occurrences of the oldChar in the current string with the newChar. The current string is returned if no occurrences of the oldChar can be found.
The second method returns a new String object that is the result of replacing all occurrences of the character sequence target in the current string with the character sequence replacement. The current string is returned if no occurrences of the target can be found.
The following methods can be used to test whether a string satisfies a given criterion:
boolean contains(CharSequence cs)
Returns true if the current string contains the specified character sequence, and false otherwise.
boolean startsWith(String prefix)
Returns true if the current string starts with the character sequence specified by parameter prefix, and false otherwise.
boolean startsWith(String prefix, int index)
Returns true if the substring of the current string at the specified index starts with the character sequence specified by parameter prefix, and false otherwise.
boolean endsWith(String suffix)
Returns true if the current string ends with the character sequence specified by parameter suffix, and false otherwise.
Examples of search and replace methods in the String class:
String funStr = “Java Jives”;
// 0123456789
int jInd1a = funStr.indexOf(‘J’); // 0
int jInd1b = funStr.indexOf(‘J’, 1); // 5
int jInd2a = funStr.lastIndexOf(‘J’); // 5
int jInd2b = funStr.lastIndexOf(‘J’, 4); // 0
String banner = “One man, One vote”;
// 01234567890123456
int subInd1a = banner.indexOf(“One”); // 0
int subInd1b = banner.indexOf(“One”, 3); // 9
int subInd2a = banner.lastIndexOf(“One”); // 9
int subInd2b = banner.lastIndexOf(“One”, 10); // 9
int subInd2c = banner.lastIndexOf(“One”, 8); // 0
int subInd2d = banner.lastIndexOf(“One”, 2); // 0
String newStr = funStr.replace(‘J’, ‘W’); // “Wava Wives”
String newBanner = banner.replace(“One”, “No”); // “No man, No vote”
boolean found1 = banner.contains(“One”); // true
boolean found2 = newBanner.contains(“One”); // false
String song = “Start me up!”;
// 012345677890
boolean found3 = song.startsWith(“Start”); // true
boolean notFound1 = song.startsWith(“start”); // false
boolean found4 = song.startsWith(“me”, 6); // true
boolean found5 = song.endsWith(“up!”); // true
boolean notFound2 = song.endsWith(“up”); // false
Leave a Reply