C#中获取StringCollection中首次出现的索引
要获取StringCollection中首次出现的索引,代码如下-
示例
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Accessories");
strCol.Add("Books");
strCol.Add("Electronics");
strCol.Add("Books");
Console.WriteLine("StringCollection 元素...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
Console.WriteLine("StringCollection 元素...UPDATED");
foreach (string res in strCol) {
Console.WriteLine(res);
}
Console.WriteLine("首次出现Books的索引? = "+strCol.IndexOf("Books"));
}
}输出结果
这将产生以下输出-
StringCollection 元素... Accessories Books Electronics Books StringCollection 元素...UPDATED Accessories Books Headphone Electronics Books 首次出现Books的索引? = 1
示例
现在让我们来看另一个示例-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "D", "H" };
Console.WriteLine("StringCollection elements...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("第5个索引处的元素 = "+strCol[5]);
Console.WriteLine("StringCollection 中的字符串计数 = " + strCol.Count);
Console.WriteLine("D首次出现的索引? = "+strCol.IndexOf("D"));
}
}输出结果
这将产生以下输出-
StringCollection 元素... A B C D E F D H 第5个索引处的元素 = F StringCollection 中的字符串计数 = 8 D首次出现的索引? = 3
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短