Convert List into Comma-Separated String

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

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

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

回答1:

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.



回答2:

You can use String.Join method to combine items:

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


回答3:

Using String.Join

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

Using Linq Aggregation

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


回答4:

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:

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:

          @{  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.



回答7:

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:

Try

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

HTH



回答9:

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

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


回答10:

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:

you can make use of google-collections.jar which has a utility class called 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

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

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



回答12:

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#



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!