Search strings : String Search : Data Types C# Source Code


Custom Search

C# Source Code » Data Types » String Search »

 

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

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Data Types
» String Search