将字符串列表组件连接成以逗号分隔的字符串

时间:2016-02-25 06:38:29

标签: c#

我有一个stringList<String>)列表。我想连接列表项并形成一个新的逗号分隔字符串。我在C#中知道。请为我提供最好的解决方案。

2 个答案:

答案 0 :(得分:3)

string[] myList = ...
string concatenated = string.Join("," myList);

答案 1 :(得分:1)

如果您需要创建 CSV 逗号分隔值)字符串

  List<String> source = new List<String>() {
    "abc",   // just a string
    "d,e",   // string which contains comma
    "x\"yz", // string which contains quotation
  };

  String result = String.Join(",", source
    .Select(x => x.Contains(',') || x.Contains('"')
      ? "\"" + String.Concat(x.Select(c => c == '"' ? "\"\"" : c.ToString())) + "\""
      : x));

  ...

  // abc,"d,e","x""yz"
  Console.Write(result);

注意引号已添加