连云港 网站 建设,腾讯企点和企业qq区别,青岛做网站皆赴青岛博采网络,公司怎么在网上推广在 C# 中#xff0c;IndexOf 是一个字符串、数组或列表的方法#xff0c;用于查找指定元素的第一个匹配项的索引。它返回一个整数值#xff0c;表示匹配项在集合中的位置#xff0c;如果未找到匹配项#xff0c;则返回 -1。
IndexOf 方法有多个重载形式#xff0c;可以根…在 C# 中IndexOf 是一个字符串、数组或列表的方法用于查找指定元素的第一个匹配项的索引。它返回一个整数值表示匹配项在集合中的位置如果未找到匹配项则返回 -1。
IndexOf 方法有多个重载形式可以根据需要选择不同的使用方式。
对于字符串
string str Hello, World!;
int index str.IndexOf(World);
Console.WriteLine(index); // 输出 7对于数组和列表
int[] numbers { 1, 2, 3, 4, 5 };
int index Array.IndexOf(numbers, 3);
Console.WriteLine(index); // 输出 2Liststring fruits new Liststring{apple, banana, orange};
int index fruits.IndexOf(banana);
Console.WriteLine(index); // 输出 1在以上示例中我使用了 IndexOf 方法来查找指定元素在字符串、数组和列表中的索引位置。