Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to List<string> in one line?

I have a string:

var names = "Brian,Joe,Chris"; 

Is there a way to convert this to a List<string> delimited by , in one line?

like image 777
Brian David Berman Avatar asked Feb 16 '11 01:02

Brian David Berman


People also ask

How do I convert a list to a string in one line?

To convert a list to a string in one line, use either of the three methods: Use the ''. join(list) method to glue together all list elements to a single string. Use the list comprehension method [str(x) for x in lst] to convert all list elements to type string.

How do I convert a string to a list of strings?

How to Convert a String to a List of Words. Another way to convert a string to a list is by using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.

How do I convert a string to a list in Python?

To convert string to list in Python, use the string split() method. The split() is a built-in Python method that splits the strings and stores them in the list.

Can we convert string into list?

Strings can be converted to lists using list() .


1 Answers

List<string> result = names.Split(new char[] { ',' }).ToList(); 

Or even cleaner by Dan's suggestion:

List<string> result = names.Split(',').ToList(); 
like image 117
Matt Greer Avatar answered Sep 22 '22 23:09

Matt Greer