C# 将 List<int> 转换为逗号分隔的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:28:10  来源:igfitidea点击:

Converting a List<int> to a comma separated string

c#collections

提问by mrblah

Is there a way to take a List and convert it into a comma separated string?

有没有办法将 List 转换为逗号分隔的字符串?

I know I can just loop and build it, but somehow I think some of you guys a more cool way of doing it?

我知道我可以循环并构建它,但不知何故,我认为你们中的一些人有一种更酷的方式来做到这一点?

I really want to learn these types of 'tricks', so please explain or link to the docs on the method you use.

我真的很想学习这些类型的“技巧”,所以请解释或链接到有关您使用的方法的文档。

采纳答案by Pavel Minaev

List<int> list = ...;
string.Join(",", list.Select(n => n.ToString()).ToArray())

回答by Yuriy Faktorovich

List<int> list = new List<int> { 1, 2, 3 };
Console.WriteLine(String.Join(",", list.Select(i => i.ToString()).ToArray()));

回答by Larsenal

My "clever" entry:

我的“聪明”条目:

        List<int> list = new List<int> { 1, 2, 3 };
        StringBuilder sb = new StringBuilder();
        var y = list.Skip(1).Aggregate(sb.Append(x.ToString()),
                    (sb1, x) =>  sb1.AppendFormat(",{0}",x));

        // A lot of mess to remove initial comma
        Console.WriteLine(y.ToString().Substring(1,y.Length - 1));

Just haven't figured how to conditionally add the comma.

只是还没想好如何有条件地添加逗号。

回答by cdiggins

For extra coolness I would make this an extension method on IEnumerable<T> so that it works on any IEnumerable:

为了更酷,我会将其作为 IEnumerable<T> 上的扩展方法,以便它适用于任何 IEnumerable:

public static class IEnumerableExtensions {
  public static string BuildString<T>(this IEnumerable<T> self, string delim = ",") {
    return string.Join(delim, self)        
  }
}

Use it as follows:

使用方法如下:

List<int> list = new List<int> { 1, 2, 3 };
Console.WriteLine(list.BuildString(", "));

回答by Eric Lippert

For approximately one gazillion solutions to a slightly more complicated version of this problem -- many of which are slow, buggy, or don't even compile -- see the comments to my article on this subject:

对于这个问题的稍微复杂一点的版本的大约一亿个解决方案——其中许多是缓慢的、有问题的,或者甚至不能编译——请参阅我关于这个主题的文章的评论:

http://blogs.msdn.com/ericlippert/archive/2009/04/15/comma-quibbling.aspx

http://blogs.msdn.com/ericlippert/archive/2009/04/15/comma-quibbling.aspx

and the StackOverflow commentary:

和 StackOverflow 评论:

Eric Lippert's challenge "comma-quibbling", best answer?

Eric Lippert 的挑战“逗号狡辩”,最佳答案?

回答by Gregory

Seems reasonablly fast.

似乎相当快。

IList<int> listItem = Enumerable.Range(0, 100000).ToList();
var result = listItem.Aggregate<int, StringBuilder, string>(new StringBuilder(), (strBuild, intVal) => { strBuild.Append(intVal); strBuild.Append(","); return strBuild; }, (strBuild) => strBuild.ToString(0, strBuild.Length - 1));

回答by Nitin Daware

Simple solution is

简单的解决办法是

List<int> list = new List<int>() {1,2,3};
string.Join<int>(",", list)

I used it just now in my code, working funtastic.

我刚刚在我的代码中使用了它,工作起来很有趣。

回答by Maurico Bello

you can use, the System.Linq library; It is more efficient:

您可以使用 System.Linq 库;它更有效:

using System.Linq;
string str =string.Join(",", MyList.Select(x => x.NombreAtributo));