简体   繁体   English

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

[英]Comma Separated string Built In .NET

I used a built in function to create comma separated string using List easily. 我使用内置函数轻松使用List创建逗号分隔的字符串。 (It is not split and join but new function) I'm not able to recollect or find it. (它不是拆分和联接,而是新功能),我无法回忆或找到它。 If some one knows about it and uses it please post a link to that. 如果有人知道并使用它,请发布一个链接。 Framework - .net 2.0 框架-.net 2.0

(It is not Join or split - I know about this, .net has new built in function to create CSV format) (它不是Join或split-我对此很了解,.net具有内置的新功能来创建CSV格式)

Check Jacob G Answer below for what I was looking for let me know your thoughts on it compared to join ;) 在下面查看Jacob G的答案,以了解我正在寻找的内容。与加入相比,让我知道您的想法;)

And whoever gave me -ve rep need to keep some patience and not hurry 无论谁给我-ve代表都需要保持耐心而不要着急

public static string SomethingElseWithComma(this IEnumerable<string> list)
{
  if(list == null)
      return null;

  return String.Join(",",list.ToArray());
}

ps. ps。 don't downvote, just having fun. 不要投反对票,只是玩得开心。

This might be what you're thinking of... You need to reference the System.Configuration dll and import the appropriate namespace. 这可能就是您在想的...您需要引用System.Configuration dll并导入适当的名称空间。

    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());

By the way, I found this class by opening up the documentation and typing the word "comma" in the index. 顺便说一句,我通过打开文档并在索引中键入“逗号”来找到此类。

EDIT 编辑
In response to your new question - Assuming that your List is already constructed, String.Join is going to be more performant. 回答您的新问题-假设您的List已经构建,String.Join的性能将会更高。 This collection just uses a StringBuilder. 该集合仅使用StringBuilder。 String.Join has a number of low-level optimization that will make it faster. String.Join有许多低级优化,可以使其更快。

(also, not terribly cool to take away the "correct answer" after you change to a new question) (同样,更改为新问题后,拿走“正确答案”也不是很酷)

Out of the box, I don't think List<T> has any methods or properties which do this. 开箱即用,我不认为List<T>具有执行此操作的任何方法或属性。 I agree with jsmith it must have been an extension method, etc. 我同意jsmith,它一定是扩展方法,等等。

//likely the best you'll do without writing your 
//own extension method or coding SomethingElse.
string.Join(", ", list.ToArray());

我相信您正在寻找String.Join方法?

String.Join(",",yourEnumerable.ToArray())

was it string.Join? 是字符串吗? MSDN MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM