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. The application in Fig. 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. We perform all searches in this example on the string letters (initialized with "abcdefghijklmabcdefghijklm") located in method Main of class StringIndexMethods.

Figure 16.5. Searching for characters and substrings in strings.

(This item is displayed on pages 775 - 776 in the print version)

 1 // Fig. 16.5: StringIndexMethods.cs
 2 // Using string searching methods.
 3 using System;
 4
 5 class StringIndexMethods
 6 {
 7 public static void Main()
 8 {
 9 string 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 starting 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 } // end method Main 66 } // end class StringIndexMethods
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 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 version of method IndexOf that takes two argumentsthe character to search for and the starting index at which the search of the string 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 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 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 of the string 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 LastIndexOf. The expression in line 22 uses the version of method LastIndexOf that takes as an argument the character for which to search. The expression in line 24 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 26 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 2944 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 4764 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 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.


Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

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