Khai báo và khởi tạo mảng -
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
}; Bây giờ, phương thức IndexOf () để tìm chỉ mục của từ “Mat” -
Array.IndexOf(str, "Mat");
Sau đây là mã -
Ví dụ
using System;
public class Demo {
public static void Main() {
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
};
Console.WriteLine("Our Array =");
for (int i = 0; i < str.Length; i++) {
string res = str[i];
Console.WriteLine(res);
}
int findIndex = Array.IndexOf(str, "Mat");
Console.Write("Element Mat found at the following index: ");
Console.WriteLine(findIndex);
}
} Đầu ra
Our Array = Cat Mat Rat Element Mat found at the following index: 1