Remove all empty elements from string array
55,596
Solution 1
You can use List.RemoveAll
:
C#
s.RemoveAll(str => String.IsNullOrEmpty(str));
VB.NET
s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
Solution 2
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
.
Solution 3
s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();
Author by
Elmo
Updated on July 09, 2022Comments
-
Elmo almost 2 years
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 aforeach
statement because that makes the code look ugly.