Remove all empty elements from string array

前端 未结 4 1130
误落风尘
误落风尘 2020-12-29 01:44

I have this:

List s = new List{\"\", \"a\", \"\", \"b\", \"\", \"c\"};

I want to remove all the empty elements

相关标签:
4条回答
  • 2020-12-29 02:17

    I write below code to remove the blank value

    List<string> s = new List<string>{"", "a", "", "b", "", "c"};
    s = s.Where(t => !string.IsNullOrWhiteSpace(t)).Distinct().ToList();
    
    0 讨论(0)
  • 2020-12-29 02:23

    You can use List.RemoveAll:

    C#

    s.RemoveAll(str => String.IsNullOrEmpty(str));
    

    VB.NET

    s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
    
    0 讨论(0)
  • 2020-12-29 02:30

    Check out with List.RemoveAll with String.IsNullOrEmpty() method;

    Indicates whether the specified string is null or an Empty string.

    s.RemoveAll(str => string.IsNullOrEmpty(str));
    

    Here is a DEMO.

    0 讨论(0)
  • 2020-12-29 02:38
    s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();
    
    0 讨论(0)
提交回复
热议问题