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

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();

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s