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?
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 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.
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.
Strings can be converted to lists using list() .
List<string> result = names.Split(new char[] { ',' }).ToList();
Or even cleaner by Dan's suggestion:
List<string> result = names.Split(',').ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With