本帖最后由 lvjayj 于 2013-8-6 21:40 编辑
这几天做入学考试题,发现很多东西都会。平常多做题也是学习的好方法啊。 List转字符串,用逗号隔开 - List<string> list = new List<string>();
- list.Add("a");
- list.Add("b");
- list.Add("c");
- string s = string.Join(",", list.ToArray());
- Console.Write(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());
- Console.Write(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)
- {
- Console.Write("*" + t + "*");
- }
复制代码这里s的分隔符是“,” - string s = "1,2,3";
- List<string> list = new List<string>(s.Split(','));
- foreach (string t in list)
- {
- Console.Write("*" + t + "*");
- }
复制代码
|