Java Reference
In-Depth Information
We can find the first and last occurrences of a character, but what about the ones in the middle? Well, there's
a variation of each of the above methods that has a second argument to specify a 'from position', from which
to start the search. To search forwards from a given position, startIndex , you would write:
index = text.indexOf('a', startIndex);
This version of the method indexOf() searches the string for the character specified by the first
argument starting with the position specified by the second argument. You could use this to find the first
' b ' that comes after the first ' a ' in a string with the statements:
int aIndex = -1; // Position of 1st 'a'
int bIndex = -1; // Position of 1st 'b' after 'a'
aIndex = text.indexOf('a'); // Find first 'a'
if(aIndex >= 0)
bIndex = text.indexOf('b', ++aIndex); // Find 1st 'b' after 1st 'a'
Once we have the index value from the initial search for ' a ', we need to check that ' a ' was really found by
verifying that aIndex is not negative. We can then search for ' b ' from the position following ' a '. As you can
see, the second argument of this version of the method indexOf() is separated from the first argument by a
comma. Since the second argument is the index position from which the search is to start, and aIndex is the
position at which ' a ' was found, we should increment aIndex to the position following ' a ' before using it in
the search for ' b ' to avoid checking for ' b ' in the position we already know contains ' a '.
If ' a ' happened to be the last character in the string, it wouldn't matter, since the indexOf() method
just returns -1 if the index value is beyond the last character in the string. If you somehow supplied a
negative index value to the method, it would simply search the whole string from the beginning.
Searching for Substrings
The methods indexOf() and lastIndexOf() also come in versions that accept a string as the first
argument, which will search for this string rather than a single character. In all other respects they work in the
same way as the character searching methods we have just seen. The complete set of indexOf() methods is:
Method
Description
indexOf(int ch)
Returns the index position of the first occurrence of the character
ch in the String for which the method is called. If the character
ch does not occur, -1 is returned.
indexOf(int ch,
int index)
Same as the method above, but with the search starting at position
index . If the value of index is outside the legal limits for the
String object, -1 is returned.
indexOf(String str)
Returns the index position of the first occurrence of the substring
str in the String object for which the method is called. If the
substring str does not occur, -1 is returned.
indexOf(String str,
int index)
Same as the method above, but with the search starting at position
index . If the value of index is outside the legal limits
for the String object, -1 is returned.
Search WWH ::




Custom Search