Putting non-empty entries from a delimited string into an array

A quick tip. I had a situation where there was a comma-delimited string, and sometimes, the last character was a comma, but no value after that. I wanted a quick way to get those values into an array, but not the empty value. I did that by using this handy little code, splitting the string, using a “where” operator to only return the items with a length greater than 0, and finally using the ToArray operator:

string airports = "LAN,GRR,DTW,FNT,";
string[] array = airports.Split(',').Where(i => i.Length > 0).ToArray();
Advertisements
  1. Long weekends | Wunderhund

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: