Mega Code Archive

 
Categories / C# by API / System
 

String IndexOf

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