Use the IndexOf() and LastIndexOf() methods to find the string 'Hello' in stringArray : Array Index « Data Structure « C# / CSharp Tutorial






using System;

class MainClass
{

  public static void Main()
  {
    string[] stringArray = {"Hello", "to", "everyone", "Hello", "all"};
    Console.WriteLine("stringArray:");
    for (int i = 0; i < stringArray.Length; i++)
    {
      Console.WriteLine("stringArray[" + i + "] = " + stringArray[i]);
    }


    int index = Array.IndexOf(stringArray, "Hello");
    Console.WriteLine("Array.IndexOf(stringArray, \"Hello\") = " + index);
    index = Array.LastIndexOf(stringArray, "Hello");
    Console.WriteLine("Array.LastIndexOf(stringArray, \"Hello\") = " + index);

  }

}
stringArray:
stringArray[0] = Hello
stringArray[1] = to
stringArray[2] = everyone
stringArray[3] = Hello
stringArray[4] = all
Array.IndexOf(stringArray, "Hello") = 0
Array.LastIndexOf(stringArray, "Hello") = 3








11.2.Array Index
11.2.1.Set array element value by index(subscript)
11.2.2.Demonstrate an array overrun
11.2.3.Assigning array reference variables
11.2.4.Use the IndexOf() and LastIndexOf() methods to find the value 1 in intArray
11.2.5.Use the IndexOf() and LastIndexOf() methods to find the string 'Hello' in stringArray
11.2.6.Swapping Data between Positions in an Array