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).

Fig. 16.5 Using string-searching methods.

Alternate View

 1    // Fig. 16.5: StringIndexMethods.cs
 2    // Using string-searching methods.
 3    using System;
 4
 5    class StringIndexMethods
 6    {
 7       static void Main()
 8       {
 9          var letters = "abcdefghijklmabcdefghijklm";
10          char[] searchLetters = {'c', 'a', '$'};
11
12          // test IndexOf to locate a character in a string
13          Console.WriteLine($"First 'c' is located at index " +
14              letters.IndexOf('c'));
15          Console.WriteLine("First 'a' starting at 1 is located at index " +
16             letters.IndexOf('a', 1));
17          Console.WriteLine("First '$' in the 5 positions starting at 3 " +
18             $"is located at index " + letters.IndexOf('$', 3, 5));
19
20          // test LastIndexOf to find a character in a string
21          Console.WriteLine($"
Last 'c' is located at index " +
22             letters.LastIndexOf('c'));
23          Console.WriteLine("Last 'a' up to position 25 is located at " +
24             "index " + letters.LastIndexOf('a', 25));
25          Console.WriteLine("Last '$' in the 5 positions ending at 15 " +
26             "is located at index " + letters.LastIndexOf('$', 15, 5));
27
28          // test IndexOf to locate a substring in a string
29          Console.WriteLine("
First "def" is located at index " +
30             letters.IndexOf("def"));
31          Console.WriteLine("First "def" starting at 7 is located at " +
32             "index " + letters.IndexOf("def", 7));
33          Console.WriteLine("First "hello" in the 15 positions " +
34             "starting at 5 is located at index " +
35             letters.IndexOf("hello", 5, 15));
36
37          // test LastIndexOf to find a substring in a string
38          Console.WriteLine("
Last "def" is located at index " +
39             letters.LastIndexOf("def"));
40          Console.WriteLine("Last "def" up to position 25 is located " +
41             "at index " + letters.LastIndexOf("def", 25));
42          Console.WriteLine("Last "hello" in the 15 positions " +
43             "ending at 20 is located at index " +
44             letters.LastIndexOf("hello", 20, 15));
45
46          // test IndexOfAny to find first occurrence of character in array
47          Console.WriteLine("
First 'c', 'a' or '$' is " +
48             "located at index " + letters.IndexOfAny(searchLetters));
49          Console.WriteLine("First 'c', 'a' or '$' starting at 7 is " +
50             "located at index " + letters.IndexOfAny(searchLetters, 7));
51          Console.WriteLine("First 'c', 'a' or '$' in the 5 positions " +
52             "starting at 7 is located at index " +
53             letters.IndexOfAny(searchLetters, 7, 5));
54
55          // test LastIndexOfAny to find last occurrence of character
56          // in array
57          Console.WriteLine("
Last 'c', 'a' or '$' is " +
58             "located at index " + letters.LastIndexOfAny(searchLetters));
59          Console.WriteLine("Last 'c', 'a' or '$' up to position 1 is " +
60             "located at index " +
61             letters.LastIndexOfAny(searchLetters, 1));
62          Console.WriteLine("Last 'c', 'a' or '$' in the 5 positions " +
63             "ending at 25 is located at index " +
64             letters.LastIndexOfAny(searchLetters, 25, 5));
65      }
66   }

First 'c' is located at index 2
First 'a' starting at 1 is located at index 13
First '$' in the 5 positions starting at 3 is located at index -1

Last 'c' is located at index 15
Last 'a' up to position 25 is located at index 13
Last '$' in the 5 positions ending at 15 is located at index -1

First "def" is located at index 3
First "def" starting at 7 is located at index 16
First "hello" in the 15 positions starting at 5 is located at index -1

Last "def" is located at index 16
Last "def" up to position 25 is located at index 16
Last "hello" in the 15 positions ending at 20 is located at index -1

First 'c', 'a' or '$' is located at index 0
First 'c', 'a' or '$' starting at 7 is located at index 13
First 'c', 'a' or '$' in the 5 positions starting at 7 is located at index -1

Last 'c', 'a' or '$' is located at index 15
Last 'c', 'a' or '$' up to position 1 is located at index 0
Last 'c', 'a' or '$' in the 5 positions ending at 25 is located at index -1

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 methods LastIndexOf and LastIndexOfAny that 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset