137

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?

4
  • 16
    I don't understand the "in one line" requirement. Shoot for writing code that is clear and understandable, not ways to fit an operation into as small a space as possible.
    – Ed S.
    Feb 16, 2011 at 1:12
  • 7
    With the advent of LINQ, "one liners" that are readable and clean have become much more common in C# IMO.
    – Matt Greer
    Feb 16, 2011 at 1:15
  • 7
    @Ed S. - While I agree that "in one line" may not be the best way to put it, single-line statements like the one Matt Greer provides tend to be very clear and understandable. My guess is he just wants to avoid using a for loop, which is more tedious, error prone, and unclear. Feb 16, 2011 at 1:18
  • 4
    I don't mean to say that one-liners are inherently harder to understand, I am just saying that it shouldn't ever be a requirement. Just do it the best way available. If that happens to be in one line, great, but it's not something that should shape the way you write your code.
    – Ed S.
    Feb 16, 2011 at 1:24

7 Answers 7

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

Or even cleaner by Dan's suggestion:

List<string> result = names.Split(',').ToList();
4
  • 13
    You may have put it there on purpose, but I always leave out the whole new char[] { } part. It's more readable that way, to me at least.
    – Dan Tao
    Feb 16, 2011 at 1:15
  • 4
    @Dan: I agree, and generally I do use the params overload. But for an answer to a question sometimes I feel like verbosity is better. Just a matter of opinion really.
    – Matt Greer
    Feb 16, 2011 at 1:17
  • 4
    ToList() doesn't seem to be avail anymore? Jul 23, 2013 at 4:18
  • 15
    Did you bring in the System.LINQ namespace?
    – Matt Greer
    Jul 23, 2013 at 15:24
51

The List<T> has a constructor that accepts an IEnumerable<T>:

List<string> listOfNames = new List<string>(names.Split(','));
1
  • Excellent - no need to import System.Linq Sep 20, 2016 at 11:07
4

I prefer this because it prevents a single item list with an empty item if your source string is empty:

  IEnumerable<string> namesList = 
      !string.isNullOrEmpty(names) ? names.Split(',') : Enumerable.Empty<string>();
3

Split a string delimited by characters and return all non-empty elements.

var names = ",Brian,Joe,Chris,,,";
var charSeparator = ",";
var result = names.Split(charSeparator, StringSplitOptions.RemoveEmptyEntries);

https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8

1

If you already have a list and want to add values from a delimited string, you can use AddRange or InsertRange. For example:

existingList.AddRange(names.Split(','));
1
string given="Welcome To Programming";
List<string> listItem= given.Split(' ').ToList();//Split according to space in the string and added into the list

output:

Welcome

To 

Programming
0

Use the Stringify.Library nuget package

//Default delimiter is ,
var split = new StringConverter().ConvertTo<List<string>>(names);

//You can also have your custom delimiter for e.g. ;
var split = new StringConverter().ConvertTo<List<string>>(names, new ConverterOptions { Delimiter = ';' });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.