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 })
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.
Answer from Richard Dalton on Stack OverflowEnjoy!
Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));
First Parameter: ","
Second Parameter: 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.
You can use String.Join method to combine items:
var str = String.Join(",", lst);
Convert a list to comma separated string without loop
apex - How can I get all elements from a list into a comma separated string? - Salesforce Stack Exchange
Convert a comma separated property to a List
Using C, convert a dynamic int array to a comma-separated string as cleanly as possible
Videos
Yup..
List<Customer__c> customers = [Select Id, LegacyCustomerNo__c FROM Customer__c WHERE state__c = 'PA'];
String[] tmp1 = New String[]{};
String[] tmp2 = New String[]{};
String idString, customernostring;
for(Customers__c c : customers__c){
tmp1.add(c.id);
tmp2.add(c.legacycustomerno__c);
}
idString = string.join(tmp1,',');
customernostring = string.join(tmp2,',');
The method you're looking for is String.join(Iterable object, String separator)
You won't be able to do what you want directly from your List<Customer__c>. Instead, you put your query directly into a for loop, and build the two lists (to be turned into comma-separated strings) at the same time.
From there, you can call String.join() to get your comma-separated strings.
List<String> idList = new List<String>();
List<String> legacyNumList = new List<String>();
for(Customer__c cust : [Select Id, LegacyCustomerNo FROM Customer__c WHERE state__c = 'PA']){
idList.add(cust.Id);
legacyNumList.add(cust.LegacyCustomerNo);
}
String commaSepIds = String.join(idList, ', ');
String commaSepLegacy = String.join(legacyNumList, ', ');
Hey guys,
I have a list that contains a property string which is comma separated.I want to create a new List from
that property .How would I go on about doing this.
I am trying something like this .(Obv not working):
List<string> picturesUrl = vehicleLists.ForEach(x=>x.PicturesUrl.Split(',')).ToList()
vehicleLists is a list with a string property PicturesUrl with comma separated values.
In .NET 4 you don't need the ToArray() call - string.Join is overloaded to accept IEnumerable<T> or just IEnumerable<string>.
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?
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.
To expand on Jon Skeets answer the code for this in .Net 4 is:
string myCommaSeperatedString = string.Join(",",ls);
This is very easy , Isn't it ?
string sCategories = string.Join(",", categories.Select(x => x.Name));
Just try with this.
By using this version of the string.Join<string> method, you can reduce copies of your collection before joining.
static string CombineList(IEnumerable categories)
{
return string.Join<string>(",", categories.Select(x => x.Name));
}
Depends on your result and description, I guess your code is something like:
b = """58738121385139B
9135A4251EF0
2B631B53C383"""
print(', '.join(b))
result:
5, 8, 7, 3, 8, 1, 2, 1, 3, 8, 5, 1, 3, 9, B,
, 9, 1, 3, 5, A, 4, 2, 5, 1, E, F, 0,
, 2, B, 6, 3, 1, B, 5, 3, C, 3, 8, 3
You should use split to achieve it
print(', '.join(b.split("\n")))
result:
58738121385139B, 9135A4251EF0, 2B631B53C383
Seems like your string consists of space-separated chunks of data, You could try this:
a = b.replace(" ", ",")