Csharp/C Sharp/Data Types/String Search

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Search strings

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Search strings. 
 
using System; 
 
public class StringSearchDemo { 
  public static void Main() { 
    string str = "C# has powerful string handling."; 
    int idx; 
 
    Console.WriteLine("str: " + str); 
 
    idx = str.IndexOf("h"); 
    Console.WriteLine("Index of first "h": " + idx); 
 
    idx = str.LastIndexOf("h"); 
    Console.WriteLine("Index of last "h": " + idx); 
 
    idx = str.IndexOf("ing"); 
    Console.WriteLine("Index of first \"ing\": " + idx); 
 
    idx = str.LastIndexOf("ing"); 
    Console.WriteLine("Index of last \"ing\": " + idx); 
 
    char[] chrs = { "a", "b", "c" }; 
    idx = str.IndexOfAny(chrs); 
    Console.WriteLine("Index of first "a", "b", or "c": " + idx); 
 
    if(str.StartsWith("C# has")) 
      Console.WriteLine("str begins with \"C# has\""); 
 
    if(str.EndsWith("ling.")) 
      Console.WriteLine("str ends with \"ling.\""); 
  } 
}


String search Demo

/*
Learning C# 
by Jesse Liberty
Publisher: O"Reilly 
ISBN: 0596003765
*/
 using System;
 namespace StringSearch
 {
     public class TesterStringSearch
     {
         public void Run()
         {
             // create some strings to work with
             string s1 = "One,Two,Three Liberty Associates, Inc. ";
             // constants for the space and comma characters
             const char Space = " ";
             const char Comma = ",";
             // array of delimiters to split the sentence with
             char[] delimiters = new char[]
             {
                 Space,
                 Comma
             };
             string output = "";
             int ctr = 1;
             // split the string and then iterate over the
             // resulting array of strings
             String[] resultArray = s1.Split(delimiters);
             foreach (String subString in resultArray)
             {
                 output += ctr++;
                 output += ": ";
                 output += subString;
                 output += "\n";
             }
             Console.WriteLine(output);
         }
         static void Main()
         {
             TesterStringSearch t = new TesterStringSearch();
             t.Run();
         }
     }
 }


String search: last index

/*
Learning C# 
by Jesse Liberty
Publisher: O"Reilly 
ISBN: 0596003765
*/
 using System;
 namespace StringSearch
 {
    public class TesterStringLastIndex
    {
       public void Run()
       {
           // create some strings to work with
           string s1 = "One Two Three Four";
           int index;
           // get the index of the last space
           index=s1.LastIndexOf(" ");
           // get the last word.
           string s2 = s1.Substring(index+1);
           // set s1 to the substring starting at 0
           // and ending at index (the start of the last word
           // thus s1 has one two three
           s1 = s1.Substring(0,index);
           // find the last space in s1 (after two)
           index = s1.LastIndexOf(" ");
           // set s3 to the substring starting at
           // index, the space after "two" plus one more
           // thus s3 = "three"
           string s3 = s1.Substring(index+1);
           // reset s1 to the substring starting at 0
           // and ending at index, thus the string "one two"
           s1 = s1.Substring(0,index);
           // reset index to the space between
           // "one" and "two"
           index = s1.LastIndexOf(" ");
           // set s4 to the substring starting one
           // space after index, thus the substring "two"
           string s4 = s1.Substring(index+1);
           // reset s1 to the substring starting at 0
           // and ending at index, thus "one"
           s1 = s1.Substring(0,index);
           // set index to the last space, but there is
           // none so index now = -1
           index = s1.LastIndexOf(" ");
           // set s5 to the substring at one past
           // the last space. there was no last space
           // so this sets s5 to the substring starting
           // at zero
           string s5 = s1.Substring(index+1);
           Console.WriteLine ("s2: {0}\ns3: {1}",s2,s3);
           Console.WriteLine ("s4: {0}\ns5: {1}\n",s4,s5);
           Console.WriteLine ("s1: {0}\n",s1);
       }
       static void Main()
       {
          TesterStringLastIndex t = new TesterStringLastIndex();
          t.Run();
       }
    }
 }


String split and search

/*
Learning C# 
by Jesse Liberty
Publisher: O"Reilly 
ISBN: 0596003765
*/
 using System;
 using System.Text;
 namespace StringSearch
 {
    public class TesterStringSearch1
    {
       public void Run()
       {
           // create some strings to work with
           string s1 = "One,Two,Three Liberty Associates, Inc.";
           // constants for the space and comma characters
           const char Space = " ";
           const char Comma = ",";
           // array of delimiters to split the sentence with
           char[] delimiters = new char[]
          {
              Space,
              Comma
          };
           // use a StringBuilder class to build the
           // output string
           StringBuilder output = new StringBuilder();
           int ctr = 1;
           // split the string and then iterate over the
           // resulting array of strings
           foreach (string subString in s1.Split(delimiters))
           {
               // AppendFormat appends a formatted string
               output.AppendFormat("{0}: {1}\n",ctr++,subString);
           }
           Console.WriteLine(output);
       }
       [STAThread]
       static void Main()
       {
          TesterStringSearch1 t = new TesterStringSearch1();
          t.Run();
       }
    }
 }


use the IndexOf() and LastIndexOf() methods to search for substrings and characters;

 

using System;
class MainClass {
    public static void Main() {
        string[] myStrings = {"To", "be", "or", "not","to", "be"};
        string myString = String.Join(".", myStrings);        
        int index = myString.IndexOf("be");
        Console.WriteLine("\"be\" first occurs at index " + index + " of myString");
        index = myString.LastIndexOf("be");
        Console.WriteLine("\"be\" last occurs at index " + index + " of myString");
        index = myString.IndexOf("b");
        Console.WriteLine(""b" first occurs at index " + index + " of myString");
        index = myString.LastIndexOf("b");
        Console.WriteLine(""b" last occurs at index " + index + " of myString");

    
    }
}