use the IndexOf() and LastIndexOf() methods to search for substrings and characters; : String Search « Data Types « C# / C Sharp






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


    
    }
}

   
  








Related examples in the same category

1.Search stringsSearch strings
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.