柚子快報(bào)激活碼778899分享:C#的IndexOf
柚子快報(bào)激活碼778899分享:C#的IndexOf
在 C# 中,IndexOf 是一個字符串、數(shù)組或列表的方法,用于查找指定元素的第一個匹配項(xiàng)的索引。它返回一個整數(shù)值,表示匹配項(xiàng)在集合中的位置,如果未找到匹配項(xiàng),則返回 -1。
IndexOf 方法有多個重載形式,可以根據(jù)需要選擇不同的使用方式。
對于字符串:
string str = "Hello, World!";
int index = str.IndexOf("World");
Console.WriteLine(index); // 輸出 7
對于數(shù)組和列表:
int[] numbers = { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(numbers, 3);
Console.WriteLine(index); // 輸出 2
List
int index = fruits.IndexOf("banana");
Console.WriteLine(index); // 輸出 1
在以上示例中,我使用了 IndexOf 方法來查找指定元素在字符串、數(shù)組和列表中的索引位置。
柚子快報(bào)激活碼778899分享:C#的IndexOf
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。