StringCollection 是专用于储存字符串的集合, 相当于字符串的动态数组.

主要成员:
/* 属性 */
Count;      //

/* 方法 */
Add();      //添加字符串
AddRange(); //添加字符串数组
Clear();    //清空
Contains(); //是否存在
CopyTo();   //复制到字符串数组
IndexOf();  //取索引, 无则 -1
Insert();   //插入
Remove();   //删除指定元素
RemoveAt(); //根据索引删除


简单测试:
protected void Button1_Click(object sender, EventArgs e)
{
    StringCollection sc = new StringCollection();

    sc.Add("AAA");
    sc.Add("BBB");

    sc.AddRange(new string[3] {"one", "two", "three"});

    sc.Insert(1, "111");
    sc.Insert(0, "000");

    string str = "";
    for (int i = 0; i < sc.Count; i++) { str += sc[i] + "; "; }

    TextBox1.Text = str; //000; AAA; 111; BBB; one; two; three; 
}

protected void Button2_Click(object sender, EventArgs e)
{
    StringCollection sc = new StringCollection();
    string[] strArr = "aaa,bbb,ccc,ddd,eee,fff".Split(',');
    sc.AddRange(strArr);

    sc.RemoveAt(2);
    sc.Remove("ddd");

    string str = "";
    foreach (string s in sc) { str += s + "; "; }
    TextBox1.Text = str; //aaa; bbb; eee; fff; 
}

 
 
 
 
 
posted on 2011-01-07 14:50  万一  阅读(1322)  评论(0)  编辑  收藏