青春几米光

点击这里给我发消息

导航

C#中ArrayList 与 string、string[]数组 的转换 (转学习:今天做项目时遇到一个arrylist转换问题,做记录)

protected string IsExistsUser(string Users)        

{            

       string[] u = Users.Split(',');            

      ArrayList lst = new ArrayList(u);            

      ArrayList lst1 = new ArrayList();
             for (int i = 0; i < u.Length; i++)            

      {                

          for (int j = i + 1; j < u.Length; j++)                

          {                    

             if (u[i].Equals(u[j]))                    

            {                        

              lst1.Add(u[i]);                    

            }                

          }            

      }           

      foreach (string s in lst1)            

      {                

         lst.Remove(s);            

      }            

      string str = string.Join(",", (string[])lst.ToArray(typeof(string)));             return str;        

}
在记几个常用的转换用法(ArrayList、string、string[]之间的转换): 1、ArrarList 转换为 string[] :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  //转换成数组

  string[] arrString = (string[])list.ToArray(typeof( string)) ;

  2、string[] 转换为 ArrarList :

  ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });

  3、ArrayList 转换为 string :

  ArrayList list = new ArrayList();

  list.Add("aaa");

  list.Add("bbb");

  //转换成数组

  string str= string.Join(",", (string[])list.ToArray(typeof( string)));

  4、string 转换为 ArrayList :

  string str="1,2,3,4,5";

  ArrayList b = new ArrayList( str.Split(',') ) ;

posted on 2013-01-24 17:46  青春几米光  阅读(354)  评论(0编辑  收藏  举报