Java Reference
In-Depth Information
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) { // Make sure you found 'a'
bIndex = text.indexOf('b', aIndex+1); // Find 1st 'b' after 1st 'a'
}
After you have the index value from the initial search for 'a' , you need to check that 'a' was really
found by verifying that aIndex is not negative. You 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. Because the second argument is the index position from which the search is to start,
and aIndex is the position at which 'a' was found, you should increment aIndex to the position following
'a' before using it in the search for 'b' to avoid checking for 'b' in the position you already know contains
'a' .
If 'a' happened to be the last character in the string, it wouldn't matter because the indexOf() method
just returns −1 if the index value is beyond the last character in the string. If you somehow supplied a neg-
ative index value to the method, it simply searches the whole string from the beginning.
Of course, you could use the indexOf() method to count how many times a particular character occurred
in a string:
int aIndex = -1; // Search start position
int count = 0; // Count of 'a' occurrences
while((aIndex = text.indexOf('a', ++aIndex)) > -1) {
++count;
}
The while loop condition expression calls the indexOf() method for the String object referenced by
text and stores the result in the variable aIndex . If the value stored is greater than -1 , it means that 'a' was
found, so the loop body executes and count is incremented. Because aIndex has -1 as its initial value, the
search starts from index position 0 in the string, which is precisely what you want. When a search reaches
the end of the string without finding 'a' , -1 is returned by the indexOf() method and the loop ends.
Searching for Substrings
The indexOf() and lastIndexOf() methods also come in versions that accept a string as the first argument,
which searches for this string rather than a single character. In all other respects they work in the same way
as the character searching methods you have just seen. I summarize the complete set of indexOf() methods
in Table 4-2 .
TABLE 4-2 : IndexOf () Methods
METHOD
DESCRIPTION
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)
Same as the preceding method, but with the search starting at position index in the string. If the
value of index is less than or equal to 0, the entire string is searched. If index is greater than or
equal to the length of the string, −1 is returned.
indexOf(int
ch,
int index)
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)
 
 
Search WWH ::




Custom Search