I have this:
List<string> s = new List<string>{"", "a", "", "b", "", "c"};
I want to remove all the empty elements ("")
from it quickly (probably through LINQ) without using a foreach
statement because that makes the code look ugly.
The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string "" contains no characters and empty elements could be None or [ ], etc.
You can use List.RemoveAll
:
C#
s.RemoveAll(str => String.IsNullOrEmpty(str));
VB.NET
s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With