在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");
Console.WriteLine("StringCollection elements...");
foreach (string res in strCol){
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
Console.WriteLine("StringCollection elements...UPDATED");
foreach (string res in strCol){
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
StringCollection elements... Accessories Books Electronics StringCollection elements...UPDATED Accessories Books Headphone Electronics
示例
现在让我们来看另一个示例-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
strCol.Add("Laptop");
strCol.Add("Desktop");
strCol.Add("Tablet");
strCol.Add("Speakers");
Console.WriteLine("StringCollection elements...");
foreach (string res in strCol){
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
strCol.Insert(3, "Alienware");
Console.WriteLine("StringCollection elements...UPDATED");
foreach (string res in strCol){
Console.WriteLine(res);
}
strCol.Insert(4, "E-Book Reader");
strCol.Insert(5, "Monitor");
strCol.Insert(6, "HDD");
strCol.Insert(7, "SSD");
Console.WriteLine("StringCollection elements...UPDATED");
foreach (string res in strCol){
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
StringCollection elements... Laptop Desktop Tablet Speakers StringCollection elements...UPDATED Laptop Desktop Headphone Alienware Tablet Speakers StringCollection elements...UPDATED Laptop Desktop Headphone Alienware E-Book Reader Monitor HDD SSD Tablet Speakers
热门推荐
4 送心的祝福语简短
10 生活老师离别祝福语简短
11 简短元宵节祝福语
12 生日祝福语 简短独特男童
13 妈妈的祝福语简短优美
14 春节新年祝福语简短幽默
15 新年顾客简短祝福语大全
16 晚来的生日祝福语简短
17 晚辈给长辈祝福语简短
18 爷爷生日会祝福语简短