要获取StringCollection中首次出现的索引,代码如下-
输出结果
这将产生以下输出-
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