Mega Code Archive

 
Categories / C# / Data Types
 

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