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 Overflow
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.
c# - Convert a list into a comma-separated string - Stack Overflow
Convert a String containing commas as separators into an Array
Converting a Comma-Separated String into a List for Filtering in Google Sheet
c# - How can I convert comma separated string into a List<int> - Stack Overflow
Videos
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.
You can use String.Join method to combine items:
var str = String.Join(",", lst);
Here is one way of doing it:
List<int> TagIds = tags.Split(',').Select(int.Parse).ToList();
If you want to include some simple validation and skip over invalid values (instead of throwing an exception), here's something that uses TryParse:
string csv = "1,2,3,4,a,5";
int mos = 0;
var intList = csv.Split(',')
.Select(m => { int.TryParse(m, out mos); return mos; })
.Where(m => m != 0)
.ToList();
//returns a list with integers: 1, 2, 3, 4, 5
EDIT: Here is an updated query based on feedback by Antoine. It calls TryParse first to filter out any bad values, and then Parse to do the actual conversion.
string csv = "1,2,3,4,a,5,0,3,r,5";
int mos = 0;
var intList = csv.Split(',')
.Where(m => int.TryParse(m, out mos))
.Select(m => int.Parse(m))
.ToList();
//returns a list with integers: 1, 2, 3, 4, 5, 0, 3, 5
Edit 2: An updated query for C# 7.0, thanks to feedback from Charles Burns. Note that we get rid of the extra mos variable with this approach, so it's a bit cleaner.
string csv = "1,2,3,4,a,5,0,3,r,5";
var intList = csv.Split(',')
.Where(m => int.TryParse(m, out _))
.Select(m => int.Parse(m))
.ToList();
Salesforce provide String class which can be used to work with string. In your case you can use Split method
String alpha = 'A, B, C, D';
List<String> lstAlpha = alpha.split(',');
System.debug(lstAlpha);
@Nebula - because pipe (|) is a regex symbol, using following code splits the string properly
String alpha = 'split | this | sentence';
List<String> lstAlpha = alpha.split('\\|');
Convert comma separated String to List
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.
Please note that this returns simply a wrapper on an array: you CANNOT for example .remove() from the resulting List. For an actual ArrayList you must further use new ArrayList<String>.
Arrays.asList returns a fixed-size List backed by the array. If you want a normal mutable java.util.ArrayList you need to do this:
List<String> list = new ArrayList<String>(Arrays.asList(string.split(" , ")));
Or, using Guava:
List<String> list = Lists.newArrayList(Splitter.on(" , ").split(string));
Using a Splitter gives you more flexibility in how you split the string and gives you the ability to, for example, skip empty strings in the results and trim results. It also has less weird behavior than String.split as well as not requiring you to split by regex (that's just one option).
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));
}
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);
You can use the str.split method.
>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
If you want to convert it to a tuple, just
>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
If you are looking to append to a list, try this:
>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
In the case of integers that are included at the string, if you want to avoid casting them to int individually you can do:
mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
It is called list comprehension, and it is based on set builder notation.
ex:
>>> mStr = "1,A,B,3,4"
>>> mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
>>> mList
>>> [1,'A','B',3,4]