Search strings : String Search « Data Types « C# / C Sharp






Search strings

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.\""); 
  } 
}

           
         
  








Related examples in the same category

1.use the IndexOf() and LastIndexOf() methods to search for substrings and characters;
2.String search: last indexString search: last index
3.String search DemoString search Demo
4.String split and searchString split and search
5.Ensure End With SemiColon
6.String Starts With
7.Checks whether the string starts with a number or not
8.Checks whether the string starts with an alphabet or not
9.Finds a given string and enclosing it with tags provided.