The Self-Taught Programmer

Recipes from real-world experience with

How to Split a String into a Generic List Rather Than an Array

leave a comment »

The Situation

You want to split a string, but the C# split() function returns a string[] array. You want a List<string> instead.

A Solution

If you are using .Net 3.5 or later, use Linq extensions:

using System.Linq;

string csvString = "a,b,c,d,e";
List<string> items = csvString.Split(',').ToList();

Written by Jim McMullen

May 29, 2013 at 5:08 pm

Posted in How-To

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: