首頁 >後端開發 >C#.Net教程 >C# LIST和STRING互相轉換

C# LIST和STRING互相轉換

高洛峰
高洛峰原創
2016-12-15 15:39:483916瀏覽

List轉字串,用逗號隔開

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

 

List list = new List();
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(string s2 = string.Join(",", b.ToArray( ));
MessageBox.Show(s1 + "rn" + s2); 

結果:1,2,,3

      a,b,,c

      a,b,,c

      a,b,c

 

不是“,”而是“, ”,後面有一個空格


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

}

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


更多C# LIST和STRING互相轉換相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn