• C# List和String互相转换


     

    List转字符串,用逗号隔开

    List<string> list = new List<string>();
    list.Add("a");
    list.Add("b");
    list.Add("c");
    //MessageBox.Show(list.);
    //LoadModel();
    string s = string.Join(",", list.ToArray());
    MessageBox.Show(s);

    List<test> list = new List<test>();
    list.Add(new test("1", "a"));
    list.Add(new test("2", "b"));
    list.Add(new test("", ""));
    list.Add(new test("3", "c"));
    var a = from o in list select o.test1;
    var b = from o in list select o.test2;
    string s1 = string.Join(",", a.ToArray());
    string s2 = string.Join(",", b.ToArray());
    MessageBox.Show(s1 + " " + s2); 

    结果:1,2,,3

          a,b,,c

    字符串转List

    这里s的分隔符不是“,”而是“, ”,后面有一个空格

    string s = "1, 2, 3";
    List<string> list = new List<string>(s.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries));
    foreach (string t in list)
    {
        MessageBox.Show("*" + t + "*");
    }

    这里s的分隔符是“,”

    string s = "1,2,3";
    List<string> list = new List<string>(s.Split(','));
    foreach (string t in list)
    {
        MessageBox.Show("*" + t + "*");
    }

    来源:http://www.cnblogs.com/eric_ibm/p/list.html

    List<int> 转 string :

    list<int>: 1,2,3,4,5,6,7  转换成字符串:“1,2,3,4,5,6,7”

    List<int> list= new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
    string depaid = string.Join(",", list);

    string  List<int>:

    string s = "1, 2, 3";
    List<string> list = new List<string>(s.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries));
    List<string> list = new List<string>(s.Split(','));//或者

     List<string> 转 List<int>

    var listOfStrings =(new[]{"4","5","6"}).ToList();
    var listOfInts = listOfStrings.Select<string,int>(q =>Convert.ToInt32(q));

    List<int> 转List<string> 

    List<int> l1 = new List<int>(new int[] { 1,2,3 } );
    List<string> l2 = l1.ConvertAll<string>(x => x.ToString());

    C#中string[]数组和list<string>:

    System.String[] str={"str","string","abc"};
    List<System.String> listS=new List<System.String>(str);

    从List<System.String>转到System.String[]

    List<System.String> listS=new List<System.String>();
    listS.Add("str");
    listS.Add("hello");
    System.String[] str=listS.ToArray();

    来源: http://www.cnblogs.com/chear/archive/2012/12/22/2828956.html

                System.String[] strArr={"str","string","abc"};

                 Func<string, string[]> func = (str) =>
                    {
                        if (str == null)
                            str = string.Empty;
                        return str.Split(',');
                    };
                    IList<System.int> ilistS = TypeParse.ToListInt(func(strArr));

  • 相关阅读:
    [引]Windows Server 2003 : 服务器群集
    周国平:(爱情)永远未完成
    企业管理常用缩写术语之中英文对照表(含解释)
    微软相关中文网站
    陈安之:NAC神经链调正术
    学会不要再争吵
    Oracle基础学习四:字符串 数字 日期 等 相关函数
    贪多嚼不烂
    frameset 框架传值点滴
    陈安之成功的十个关键
  • 原文地址:https://www.cnblogs.com/pokay/p/4478460.html
Copyright © 2020-2023  润新知