Archive for May, 2015

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 Comment