एक सरणी घोषित और आरंभ करें:
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
};
अब, शब्द "Mat" का इंडेक्स खोजने के लिए IndexOf() विधि का उपयोग करें:
Array.IndexOf(str, "Mat");
निम्न कोड है:
उदाहरण
using System;
public class Demo {
public static void Main() {
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
};
Console.WriteLine("हमारी सरणी =");
for (int i = 0; i < str.Length; i++) {
string res = str[i];
Console.WriteLine(res);
}
int findIndex = Array.IndexOf(str, "Mat");
Console.Write("तत्व Mat निम्न इंडेक्स पर मिला: ");
Console.WriteLine(findIndex);
}
}
आउटपुट
हमारी सरणी = Cat Mat Rat तत्व Mat निम्न इंडेक्स पर मिला: 1