C# 程式以在字串中查詢單詞的索引
宣告並初始化一個數組 -
string[] str = new string[] {
"Cat",
"Mat",
"Rat"
};現在,我們使用 IndexOf() 方法查詢單詞“Mat”的索引 -
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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP