代码之家  ›  专栏  ›  技术社区  ›  rs.

NET中内置的逗号分隔字符串

  •  0
  • rs.  · 技术社区  · 16 年前

    我使用一个内置函数轻松地使用List创建逗号分隔的字符串。 (这不是拆分和联接,而是新函数)我无法回忆或找到它。如果有人知道并使用它,请发布一个链接。框架-.NET2.0

    (这不是加入或拆分-我知道,.net有新的内置函数来创建CSV格式)

    不管是谁给了我-ve代表,都需要保持耐心,不要着急

    7 回复  |  直到 11 年前
        1
  •  5
  •   Jacob G    16 年前

        List<string> temp = new List<string>();
        temp.Add("a");
        temp.Add("b");
        temp.Add("c");
        CommaDelimitedStringCollection cdsc = new CommaDelimitedStringCollection();
        cdsc.AddRange(temp.ToArray());
        Console.WriteLine(cdsc.ToString());
    

    顺便说一下,我通过打开文档并在索引中键入单词“逗号”找到了这个类。


    在回答你的新问题时,假设你的列表已经构建好,String.Join将会更有效。此集合仅使用StringBuilder。Join有许多低级优化,可以使它更快。

    (另外,在你换一个新问题后,去掉“正确答案”也不是很酷)

        2
  •  6
  •   Stan R.    16 年前
    public static string SomethingElseWithComma(this IEnumerable<string> list)
    {
      if(list == null)
          return null;
    
      return String.Join(",",list.ToArray());
    }
    

    注:不要投反对票,只是玩得开心。

        3
  •  3
  •   KP.    16 年前

    开箱即用,我不认为 List<T> 具有执行此操作的任何方法或属性。我同意jsmith的观点,它一定是一种扩展方法,等等。

    //likely the best you'll do without writing your 
    //own extension method or coding SomethingElse.
    string.Join(", ", list.ToArray());
    
        4
  •  0
  •   Otávio Décio    16 年前

        5
  •  0
  •   Gregoire    16 年前
    String.Join(",",yourEnumerable.ToArray())