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?
categories = ['sprots', 'news'];
categoriesList = ", ".join(categories)
print(categoriesList)
This is the output: sprots, news
You can use String.Join for this if you are using .NET framework> 4.0.
var result= String.Join(",", yourList);
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
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()));
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);}
@{ 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.
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!
Try
Console.WriteLine((string.Join(",", lst.Select(x=>x.ToString()).ToArray())));
HTH
You can separate list entities by a comma like this:
//phones is a list of PhoneModel
var phoneNumbers = phones.Select(m => m.PhoneNumber)
.Aggregate(new StringBuilder(),
(current, next) => current.Append(next).Append(" , ")).ToString();
// Remove the trailing comma and space
if (phoneNumbers.Length > 1)
phoneNumbers = phoneNumbers.Remove(phoneNumbers.Length - 2, 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());
you can also override ToString() if your list item have more than one string
public class ListItem
{
public string string1 { get; set; }
public string string2 { get; set; }
public string string3 { get; set; }
public override string ToString()
{
return string.Join(
","
, string1
, string2
, string3);
}
}
to get csv string:
ListItem item = new ListItem();
item.string1 = "string1";
item.string2 = "string2";
item.string3 = "string3";
List<ListItem> list = new List<ListItem>();
list.Add(item);
string strinCSV = (string.Join("\n", list.Select(x => x.ToString()).ToArray()));
Using String.Join
string.Join<string>(",", lst );
Using Linq
Aggregation
lst .Aggregate((a, x) => a + "," + x);
You can use String.Join method to combine items:
var str = String.Join(",", lst);
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;
Source: Stackoverflow.com