将' List '转换为逗号分隔的字符串

时间:2022-02-19 21:27:17

Is there a fast way to convert List<string> to a comma-separated string in C#?

是否有一种快速的方法将List 转换为c#中的逗号分隔字符串?

I do it like this but Maybe there is a faster or more efficient way?

我是这样做的,但也许有更快或更有效的方法?

List<string> ls = new List<string>();
ls.Add("one");
ls.Add("two");
string type = string.Join(",", ls.ToArray());

PS: Searched on this site but most solutions are for Java or Python

PS:在这个站点上搜索,但是大多数解决方案都是针对Java或Python的

6 个解决方案

#1


100  

In .NET 4 you don't need the ToArray() call - string.Join is overloaded to accept IEnumerable<T> or just IEnumerable<string>.

在。net 4中,不需要ToArray()调用字符串。连接被重载以接受IEnumerable 或仅IEnumerable

There are potentially more efficient ways of doing it before .NET 4, but do you really need them? Is this actually a bottleneck in your code?

在。net 4之前有更有效的方法,但是你真的需要它们吗?这实际上是代码中的瓶颈吗?

You could iterate over the list, work out the final size, allocate a StringBuilder of exactly the right size, then do the join yourself. That would avoid the extra array being built for little reason - but it wouldn't save much time and it would be a lot more code.

您可以遍历列表,计算出最终的大小,分配一个大小完全正确的StringBuilder,然后自己进行连接。这样就可以避免不必要地构建额外的数组——但这不会节省很多时间,而且需要更多的代码。

#2


13  

The following will result in a comma separated list. Be sure to include a using statement for System.Linq

下面将产生一个逗号分隔的列表。确保包含System.Linq的use语句

List<string> ls = new List<string>();
ls.Add("one");
ls.Add("two");
string type = ls.Aggregate((x,y) => x + "," + y);

will yield one,two

将产生一个,两个

if you need a space after the comma, simply change the last line to string type = ls.Aggregate((x,y) => x + ", " + y);

如果在逗号后需要空格,只需将最后一行更改为string type = ls.Aggregate((x,y) => x + ", " + y ");

#3


7  

To expand on Jon Skeets answer the code for this in .Net 4 is:

在。net 4中,Jon Skeets回答这个问题的代码是:

string myCommaSeperatedString = string.Join(",",ls);

#4


3  

Follow this:

遵循这个:

       List<string> name = new List<string>();   

        name.Add("Latif");

        name.Add("Ram");

        name.Add("Adam");
        string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));

#5


0  

That's the way I'd prefer to see if I was maintaining your code. If you manage to find a faster solution, it's going to be very esoteric, and you should really bury it inside of a method that describes what it does.

这就是我希望看到我是否维护了您的代码的方式。如果你设法找到一个更快的解决方案,它将是非常深奥的,你应该把它隐藏在描述它的功能的方法中。

(does it still work without the ToArray)?

(没有ToArray它还能工作吗)?

#6


0  

static void Main(string[] args)
{
   List<string> listStrings = new List<string>(){ "C#", "Asp.Net", "SQL Server", "PHP", "Angular"};
   string CommaSeparateString = GenerateCommaSeparateStringFromList(listStrings);
   Console.Write(CommaSeparateString);
   Console.ReadKey();
}

private static string GenerateCommaSeparateStringFromList(List<string> listStrings)
{
   return String.Join(",", listStrings);  
}

Convert a list of string to comma separated string C#.

将字符串列表转换为逗号分隔的字符串c#。

#1


100  

In .NET 4 you don't need the ToArray() call - string.Join is overloaded to accept IEnumerable<T> or just IEnumerable<string>.

在。net 4中,不需要ToArray()调用字符串。连接被重载以接受IEnumerable 或仅IEnumerable

There are potentially more efficient ways of doing it before .NET 4, but do you really need them? Is this actually a bottleneck in your code?

在。net 4之前有更有效的方法,但是你真的需要它们吗?这实际上是代码中的瓶颈吗?

You could iterate over the list, work out the final size, allocate a StringBuilder of exactly the right size, then do the join yourself. That would avoid the extra array being built for little reason - but it wouldn't save much time and it would be a lot more code.

您可以遍历列表,计算出最终的大小,分配一个大小完全正确的StringBuilder,然后自己进行连接。这样就可以避免不必要地构建额外的数组——但这不会节省很多时间,而且需要更多的代码。

#2


13  

The following will result in a comma separated list. Be sure to include a using statement for System.Linq

下面将产生一个逗号分隔的列表。确保包含System.Linq的use语句

List<string> ls = new List<string>();
ls.Add("one");
ls.Add("two");
string type = ls.Aggregate((x,y) => x + "," + y);

will yield one,two

将产生一个,两个

if you need a space after the comma, simply change the last line to string type = ls.Aggregate((x,y) => x + ", " + y);

如果在逗号后需要空格,只需将最后一行更改为string type = ls.Aggregate((x,y) => x + ", " + y ");

#3


7  

To expand on Jon Skeets answer the code for this in .Net 4 is:

在。net 4中,Jon Skeets回答这个问题的代码是:

string myCommaSeperatedString = string.Join(",",ls);

#4


3  

Follow this:

遵循这个:

       List<string> name = new List<string>();   

        name.Add("Latif");

        name.Add("Ram");

        name.Add("Adam");
        string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));

#5


0  

That's the way I'd prefer to see if I was maintaining your code. If you manage to find a faster solution, it's going to be very esoteric, and you should really bury it inside of a method that describes what it does.

这就是我希望看到我是否维护了您的代码的方式。如果你设法找到一个更快的解决方案,它将是非常深奥的,你应该把它隐藏在描述它的功能的方法中。

(does it still work without the ToArray)?

(没有ToArray它还能工作吗)?

#6


0  

static void Main(string[] args)
{
   List<string> listStrings = new List<string>(){ "C#", "Asp.Net", "SQL Server", "PHP", "Angular"};
   string CommaSeparateString = GenerateCommaSeparateStringFromList(listStrings);
   Console.Write(CommaSeparateString);
   Console.ReadKey();
}

private static string GenerateCommaSeparateStringFromList(List<string> listStrings)
{
   return String.Join(",", listStrings);  
}

Convert a list of string to comma separated string C#.

将字符串列表转换为逗号分隔的字符串c#。