Getting rid of null/empty string values in a C# array

前端 未结 5 1498
自闭症患者
自闭症患者 2020-12-14 06:40

I have a program where an array gets its data using string.Split(char[] delimiter). (using \';\' as delimiter.)

Some of the values, though, are null. I.e. the string

5条回答
  •  抹茶落季
    2020-12-14 07:17

    You could use the Where linq extension method to only return the non-null or empty values.

    string someString = "1;2;;3;";
    
    IEnumerable myResults = someString.Split(';').Where(s => !string.IsNullOrEmpty(s));
    

提交回复
热议问题