Friday, 17 August 2007

Operators and Assignments (Part 10)

Strings - cont

Searching for Substrings
There are two methods available in the class String, that will search a string, indexof() and lastIndexOf(). They both have four different versions to provide a range of possibilities.

Method - Description
indexOf(int ch) - Returns the index within this string of the first occurrence of the specified character ch. If the character, ch doesn’t occur, -1 is returned.

indexOf(int ch, int index) - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. If the value of the index is outside the legal limits for the String object, -1 is returned.

indexOf(String str) - Returns the index within this string of the first occurrence of the specified substring str. If it does not occur as a substring, -1 is returned.

indexOf(String str, int index) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If the value of the index is outside the legal limits for the String object, -1 is returned.

lastIndexOf(int ch) - Returns the index within this string of the last occurrence of the specified character. If the character, ch doesn’t occur, -1 is returned.

lastIndexOf(int ch, int index) - Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. If index is negative -1 is returned. If the character does not occur at or before the index then –1 is returned.

lastIndexOf(String str) - Returns the index within this string of the rightmost occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

lastIndexOf(String str, int index) - Returns the index within this string of the last occurrence of the specified substring. The returned index indicates the start of the substring. If it does not occur as a substring starting at index or earlier, -1 is returned.

No comments:

Post a Comment