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

image_pdfimage_print
   
 


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


    
    }
}

    


This entry was posted in Data Types. Bookmark the permalink.