Section 16.6. Locating Characters and Substrings in Strings


16.6. Locating Characters and Substrings in Strings

In many applications, it is 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 of the many versions of String methods IndexOf, IndexOfAny, LastIndexOf and LastIndexOfAny, which search for a specified character or substring in a String. This example searches the String letters which is initialized with "abcdefghijklmabcdefghijklm" (line 6).

Figure 16.5. Searching for characters and substrings in Strings.

  1  ' Fig. 16.5: StringIndexMethods.vb  2  ' Using String searching methods.  3  4  Module StringIndexMethods  5     Sub Main()  6        Dim letters As String = "abcdefghijklmabcdefghijklm"  7        Dim searchLetters As Char () = {"c"c, "a"c, "$"c}  8  9        ' test IndexOf to find a character in a string 10        Console.WriteLine("First 'c' is located at index " & _ 11           letters.IndexOf("c"c)) 12        Console.WriteLine("First 'a' starting at 1 is located at index " _ 13           & letters.IndexOf("a"c, 1)) 14        Console.WriteLine("First '$' in the 5 positions starting at 3 " & _ 15           "is located at index " & letters.IndexOf("$"c, 3, 5)) 16 17        ' test LastIndexOf to find a character in a string 18        Console.WriteLine(vbCrLf & "Last 'c' is located at index " & _ 19           letters.LastIndexOf("c"c)) 20        Console.WriteLine(,"Last 'a' up to position 25 is located at " $ & _ 21           "index " & letters.LastIndexOf("a"c, 25)) 22        Console.WriteLine("Last '$' in the 5 positions starting at 15 " & _ 23           "is located at index " & letters.LastIndexOf("$"c, 15, 5)) 24 25        ' test IndexOf to find a substring in a string 26        Console.WriteLine(vbCrLf & _ 27           "First ""def"" is located at index " & letters.IndexOf("def")) 28        Console.WriteLine("First ""def"" starting at 7 is located at " & _ 29           "index " & letters.IndexOf("def", 7)) 30        Console.WriteLine("First ""hello"" in the 15 positions " & _ 31           "starting at 5 is located at index " & _ 32           letters.IndexOf("hello", 5, 15)) 33 34        ' test LastIndexOf to find a substring in a string 35        Console.WriteLine(vbCrLf & "Last ""def"" is located at index " & _ 36           letters.LastIndexOf("def")) 37        Console.WriteLine("Last ""def"" up to position 25 is located " & _ 38           "at index" & letters.LastIndexOf("def", 25)) 39        Console.WriteLine("Last ""hello"" in the 15 positions " & _ 40           "ending at 20 is located at index " & _ 41           letters.LastIndexOf("hello", 20, 15)) 42 43        ' test IndexOfAny to find first occurrence of character in array 44        Console.WriteLine(vbCrLf & "First 'c', 'a' or '$' is " & _ 45           "located at index " & letters.IndexOfAny(searchLetters)) 46        Console.WriteLine("First 'c', 'a' or '$' starting at 7 is " & _ 47           "located at index " & letters.IndexOfAny(searchLetters, 7)) 48        Console.WriteLine("First 'c', 'a' or '$' in the 5 positions " & _ 49           "starting at 7 is located at index " & _ 50           letters.IndexOfAny(searchLetters, 7, 5)) 51 52        ' test LastIndexOfAny to find last occurrence of character 53        ' in array 54        Console.WriteLine(vbCrLf & "Last 'c', 'a' or '$' is " & _ 55           "located at index " & letters.LastIndexOfAny(searchLetters)) 56        Console.WriteLine("Last 'c', 'a' or '$' up to position 1 is " & _ 57           "located at index " & letters.LastIndexOfAny(searchLetters, 1)) 58        Console.WriteLine("Last 'c', 'a' or '$' in the 5 positions " & _ 59           "ending at 25 is located at index " & _ 60           letters.LastIndexOfAny(searchLetters, 25, 5)) 61     End Sub ' Main 62  End Module ' StringIndexMethods 

[View full width]

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 starting 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 11, 13 and 15 use method IndexOf to locate the first occurrence of a character in a String. If it finds a character, IndexOf returns the index of the specified character in the String; otherwise, IndexOf returns 1. Line 13 uses a version of method IndexOf that takes two argumentsthe character to search for and the starting index in the String at which the search should begin. The method does not examine any characters before the starting index (in this case, 1). Line 15 uses another version of method IndexOf that takes three argumentsthe character to search for, the index at which to start searching and the number of characters to search.

Lines 19, 21 and 23 use method LastIndexOf to locate the last occurrence of a character in a String. Method LastIndexOf searches from the end of the String toward the beginning. 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 LastIndexOf. Line 19 uses the version of method LastIndexOf that takes as an argument the character for which to search. Line 21 uses the version of method LastIndexOf that takes two argumentsthe character for which to search and the highest index from which to begin searching backward for the character. The expression in line 23 uses a third version of method LastIndexOf that takes three argumentsthe 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 2641 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 the ones described above except that they search for sequences of characters (or substrings) that are specified by their String arguments.

Lines 4460 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 occurrence of any of the characters in the character array argument.

Common Programming Error 16.3

In the overloaded methods LastIndexOf and LastIndexOfAny that take three parameters, the second argument must be greater than or equal to the third argument; otherwise, an exception occurs. This might seem counterintuitive, but remember that the search moves from the end of the string toward the beginning.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net