Home > Backend Development > C#.Net Tutorial > C# Convert LIST and STRING to each other

C# Convert LIST and STRING to each other

高洛峰
Release: 2016-12-15 15:39:48
Original
3937 people have browsed it

List to string, separated by commas

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( ));
MessageBox.Show(s1 + "rn" + s2);

Result: 1,2,,3

a,b,,c

String to List

The delimiter of s here It's not "," but ", ", followed by a space

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

The delimiter of s here is ","

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


For more articles related to the conversion of C# LIST and STRING, please pay attention to the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template