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 + "\r\n" + 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));

 

posted @ 2015-05-05 10:28  龙卷风清扬  阅读(9940)  评论(0编辑  收藏  举报