16.6 Locating Characters and Substrings in strings
In many apps, it’s necessary to search for a character or set of characters in a string. For example, a programmer creating a word processor would want to provide capabilities for searching through documents. Figure 16.5 demonstrates some versions of string methods IndexOf, IndexOfAny, LastIndexOf and LastIndexOfAny, which search for a specified character or substring in a string. We perform all searches in this example on the string letters (line 9).
Lines 14, 16 and 18 use method IndexOf to locate the first occurrence of a character or substring in a string. If it finds a character, IndexOf returns the index of the specified character in the string; otherwise, IndexOf returns –1. The expression in line 16 uses a method IndexOf with two arguments—the character to search for and the starting index at which the search should begin. The method does not examine any characters that occur prior to the starting index (in this case, 1). The expression in line 18 uses method IndexOf with three arguments—the character to search for, the index at which to start searching and the number of characters to search.
Lines 22, 24 and 26 use method LastIndexOf to locate the last occurrence of a character in a string. Method LastIndexOf performs the search from the end to the beginning of the string. If it finds the character, LastIndexOf returns the index of the specified character in the string; otherwise, LastIndexOf returns –1. There are three versions of method LastIndexOf. The expression in line 22 uses the version that takes as an argument the character for which to search. The expression in line 24 uses the version that takes two arguments—the character for which to search and the highest index from which to begin searching backward for the character. The expression in line 26 uses a third version of method LastIndexOf that takes three arguments—the character for which to search, the starting index from which to start searching backward and the number of characters (the portion of the string) to search.
Lines 29–44 use versions of IndexOf and LastIndexOf that take a string instead of a character as the first argument. These versions of the methods perform identically to those described above except that they search for sequences of characters (or substrings) that are specified by their string arguments.
Lines 47–64 use methods IndexOfAny and LastIndexOfAny, which take an array of characters as the first argument. These versions of the methods also perform identically to those described above, except that they return the index of the first or last occurrence of any of the characters in the character-array argument, respectively.
Common Programming Error 16.2
In the overloaded methodsLastIndexOf andLastIndexOfAnythat take three parameters, the second argument must be greater than or equal to the third. This might seem counterintuitive, but remember that the search moves from the end of the string toward the start of the string.