逗号分隔字符串内置 .NET

发布于 2024-08-21 12:50:51 字数 269 浏览 5 评论 0原文

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

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

检查下面的 Jacob G 答案以了解我正在寻找的内容,让我知道您的想法与加入相比;)

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

I used a built in function to create comma separated string using List easily.
(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

(It is not Join or split - I know about this, .net has new built in function to create CSV format)

Check Jacob G Answer below for what I was looking for let me know your thoughts on it compared to join ;)

And whoever gave me -ve rep need to keep some patience and not hurry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倒数 2024-08-28 12:50:51
public static string SomethingElseWithComma(this IEnumerable<string> list)
{
  if(list == null)
      return null;

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

附:不要投票,只是玩得开心。

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

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

ps. don't downvote, just having fun.

小草泠泠 2024-08-28 12:50:51

这可能就是您正在考虑的...您需要引用 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());

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

编辑
回答您的新问题 - 假设您的列表已经构建, String.Join 的性能将会更高。该集合仅使用 StringBuilder。 String.Join 有许多低级优化,可以使其速度更快。

(另外,在换成新问题后拿走“正确答案”也不是很酷)

This might be what you're thinking of... You need to reference the System.Configuration dll and import the appropriate namespace.

    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. This collection just uses a StringBuilder. String.Join has a number of low-level optimization that will make it faster.

(also, not terribly cool to take away the "correct answer" after you change to a new question)

丢了幸福的猪 2024-08-28 12:50:51

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

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

Out of the box, I don't think List<T> has any methods or properties which do this. I agree with jsmith it must have been an extension method, etc.

//likely the best you'll do without writing your 
//own extension method or coding SomethingElse.
string.Join(", ", list.ToArray());
一萌ing 2024-08-28 12:50:51

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

I believe you are looking for the String.Join method?

携君以终年 2024-08-28 12:50:51
String.Join(",",yourEnumerable.ToArray())
String.Join(",",yourEnumerable.ToArray())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文