C# 将列表转换为逗号分隔的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14959824/
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
Convert List into Comma-Separated String
提问by SKJ
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<uint>
为逗号分隔的字符串的最简单方法是什么?
采纳答案by Richard Dalton
Enjoy!
享受!
Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));
First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })
第一个参数:","
第二个参数:new List<uint> { 1, 2, 3, 4, 5 })
String.Joinwill 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.
String.Join将一个列表作为第二个参数,并使用作为第一个参数传递的字符串将所有元素连接成一个字符串。
回答by Sergey Berezovskiy
You can use String.Joinmethod to combine items:
您可以使用String.Join方法来组合项目:
var str = String.Join(",", lst);
回答by MuhammadHani
Using String.Join
使用 String.Join
string.Join<string>(",", lst );
Using Linq
Aggregation
使用 Linq
Aggregation
lst .Aggregate((a, x) => a + "," + x);
回答by Jay
Try
尝试
Console.WriteLine((string.Join(",", lst.Select(x=>x.ToString()).ToArray())));
HTH
HTH
回答by Salim Latif Waigaonkar
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()));
回答by Syed Qasim Abbas
@{ 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 View 中使用来评估和打印以逗号分隔的所有角色。
回答by satish
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-terminal.blogspot.in/2015/08/convert-collection-into-comma-separated.html
回答by Gaurang Dhandhukiya
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());
回答by Abdus Salam Azad
You can use String.Join for this if you are using .NET framework> 4.0.
如果您使用 .NET 框架> 4.0,您可以为此使用 String.Join。
var result= String.Join(",", yourList);
回答by Ajeet Singh
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;