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

时间:2022-06-04 20:48:14

My code is as below:

我的代码如下:

public void ReadListItem()
{
     List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 };
     string str = string.Empty;
     foreach (var item in lst)
         str = str + item + ",";

     str = str.Remove(str.Length - 1);
     Console.WriteLine(str);
}

Output: 1,2,3,4,5

输出:1、2、3、4、5所示

What is the most simple way to convert the List<uint> into a comma-separated string?

将List 转换为逗号分隔字符串的最简单方法是什么?

12 个解决方案

#1


209  

Enjoy!

享受吧!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

字符串。Join将使用列表作为第二个参数,并使用作为第一个参数传递的字符串将所有元素连接到一个字符串中。

#2


55  

You can use String.Join method to combine items:

您可以使用字符串。连接方法合并项目:

var str = String.Join(",", lst);

#3


23  

Using String.Join

使用String.Join

string.Join<string>(",", lst );

Using Linq Aggregation

使用Linq聚合

lst .Aggregate((a, x) => a + "," + x);

#4


8  

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


6  

If you have a collection of ints:

如果你有一批小型旅馆:

List<int> customerIds= new List<int>() { 1,2,3,3,4,5,6,7,8,9 };  

You can use string.Join to get a string:

您可以使用字符串。连接获取一个字符串:

var result = String.Join(",", customerIds);

Enjoy!

享受吧!

#6


3  

          @{  var result = string.Join(",", @user.UserRoles.Select(x => x.Role.RoleName));
              @result

           }

I used in MVC Razor View to evaluate and print all roles separated by commas.

我在MVC Razor视图中使用来评估和打印所有用逗号分隔的角色。

#7


2  

You can refer below example for getting a comma separated string array from list.

您可以参考下面的示例,从列表中获取一个逗号分隔的字符串数组。

Example:

例子:

List<string> testList= new List<string>();
testList.Add("Apple"); // Add string 1
testList.Add("Banana"); // 2
testList.Add("Mango"); // 3
testList.Add("Blue Berry"); // 4
testList.Add("Water Melon"); // 5

string JoinDataString = string.Join(",", testList.ToArray());

#8


1  

Try

试一试

Console.WriteLine((string.Join(",", lst.Select(x=>x.ToString()).ToArray())));

HTH

HTH

#9


1  

You can use String.Join for this if you are using .NET framework> 4.0.

您可以使用字符串。如果您正在使用。net framework> 4.0,请加入。

var result= String.Join(",", yourList);

#10


1  

We can try like this to separate list enties by comma

我们可以试着用逗号分隔链表

string stations = 
haul.Routes != null && haul.Routes.Count > 0 ?String.Join(",",haul.Routes.Select(y => 
y.RouteCode).ToList()) : string.Empty;

#11


0  

you can make use of google-collections.jar which has a utility class called Joiner

你可以使用google-collections。jar,它有一个叫做Joiner的实用类

 String commaSepString=Joiner.on(",").join(lst);

or

you can use StringUtils class which has function called join.To make use of StringUtils class,you need to use common-lang3.jar

可以使用StringUtils类,该类具有名为join的函数。要使用StringUtils类,需要使用common lang3.jar

String commaSepString=StringUtils.join(lst, ',');

for reference, refer this link http://techno-terminal.blogspot.in/2015/08/convert-collection-into-comma-separated.html

为参考,请参考此链接http://techno .blogspot.in/2015/08/convert-collection-into-逗号分隔。html。

#12


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


209  

Enjoy!

享受吧!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

字符串。Join将使用列表作为第二个参数,并使用作为第一个参数传递的字符串将所有元素连接到一个字符串中。

#2


55  

You can use String.Join method to combine items:

您可以使用字符串。连接方法合并项目:

var str = String.Join(",", lst);

#3


23  

Using String.Join

使用String.Join

string.Join<string>(",", lst );

Using Linq Aggregation

使用Linq聚合

lst .Aggregate((a, x) => a + "," + x);

#4


8  

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


6  

If you have a collection of ints:

如果你有一批小型旅馆:

List<int> customerIds= new List<int>() { 1,2,3,3,4,5,6,7,8,9 };  

You can use string.Join to get a string:

您可以使用字符串。连接获取一个字符串:

var result = String.Join(",", customerIds);

Enjoy!

享受吧!

#6


3  

          @{  var result = string.Join(",", @user.UserRoles.Select(x => x.Role.RoleName));
              @result

           }

I used in MVC Razor View to evaluate and print all roles separated by commas.

我在MVC Razor视图中使用来评估和打印所有用逗号分隔的角色。

#7


2  

You can refer below example for getting a comma separated string array from list.

您可以参考下面的示例,从列表中获取一个逗号分隔的字符串数组。

Example:

例子:

List<string> testList= new List<string>();
testList.Add("Apple"); // Add string 1
testList.Add("Banana"); // 2
testList.Add("Mango"); // 3
testList.Add("Blue Berry"); // 4
testList.Add("Water Melon"); // 5

string JoinDataString = string.Join(",", testList.ToArray());

#8


1  

Try

试一试

Console.WriteLine((string.Join(",", lst.Select(x=>x.ToString()).ToArray())));

HTH

HTH

#9


1  

You can use String.Join for this if you are using .NET framework> 4.0.

您可以使用字符串。如果您正在使用。net framework> 4.0,请加入。

var result= String.Join(",", yourList);

#10


1  

We can try like this to separate list enties by comma

我们可以试着用逗号分隔链表

string stations = 
haul.Routes != null && haul.Routes.Count > 0 ?String.Join(",",haul.Routes.Select(y => 
y.RouteCode).ToList()) : string.Empty;

#11


0  

you can make use of google-collections.jar which has a utility class called Joiner

你可以使用google-collections。jar,它有一个叫做Joiner的实用类

 String commaSepString=Joiner.on(",").join(lst);

or

you can use StringUtils class which has function called join.To make use of StringUtils class,you need to use common-lang3.jar

可以使用StringUtils类,该类具有名为join的函数。要使用StringUtils类,需要使用common lang3.jar

String commaSepString=StringUtils.join(lst, ',');

for reference, refer this link http://techno-terminal.blogspot.in/2015/08/convert-collection-into-comma-separated.html

为参考,请参考此链接http://techno .blogspot.in/2015/08/convert-collection-into-逗号分隔。html。

#12


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#