Archive for May, 2015
Putting non-empty entries from a delimited string into an array
Posted by scottstoecker in .NET, C# on May 21, 2015
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();
Recent Comments