배열 선언 및 초기화 -
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
}; 이제 "Mat"라는 단어의 인덱스를 찾는 ue IndexOf() 메서드 -
Array.IndexOf(str, "Mat");
다음은 코드입니다 -
예
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);
}
} 출력
Our Array = Cat Mat Rat Element Mat found at the following index: 1