Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove an empty string from a list of empty strings in C#?
In C#, you can remove empty strings from a list using several methods. Empty strings can be truly empty ("") or contain only whitespace characters (" "). This article demonstrates different approaches to remove these empty or whitespace-only strings from a list.
Syntax
Using RemoveAll() method to remove empty strings −
list.RemoveAll(string.IsNullOrEmpty);
Using RemoveAll() with lambda expression to remove empty and whitespace strings −
list.RemoveAll(x => string.IsNullOrWhiteSpace(x));
Using LINQ Where() method to filter out empty strings −
var filteredList = list.Where(x => !string.IsNullOrEmpty(x)).ToList();
Using RemoveAll() Method
The RemoveAll() method removes all elements that match the specified condition. This is the most efficient way to remove empty strings directly from the original list −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> myList = new List<string>() {
"Hello",
"",
"World",
"",
"C#"
};
Console.WriteLine("Original list:");
foreach (string item in myList) {
Console.WriteLine("'" + item + "'");
}
myList.RemoveAll(string.IsNullOrEmpty);
Console.WriteLine("\nAfter removing empty strings:");
foreach (string item in myList) {
Console.WriteLine("'" + item + "'");
}
}
}
The output of the above code is −
Original list: 'Hello' '' 'World' '' 'C#' After removing empty strings: 'Hello' 'World' 'C#'
Removing Empty and Whitespace Strings
To remove both empty strings and strings containing only whitespace characters, use string.IsNullOrWhiteSpace() −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> myList = new List<string>() {
"Hello",
"",
" ",
"World",
"\t",
"C#"
};
Console.WriteLine("Original list:");
foreach (string item in myList) {
Console.WriteLine("'" + item + "'");
}
myList.RemoveAll(x => string.IsNullOrWhiteSpace(x));
Console.WriteLine("\nAfter removing empty and whitespace strings:");
foreach (string item in myList) {
Console.WriteLine("'" + item + "'");
}
}
}
The output of the above code is −
Original list: 'Hello' '' ' ' 'World' ' ' 'C#' After removing empty and whitespace strings: 'Hello' 'World' 'C#'
Using LINQ Where() Method
The LINQ Where() method creates a new list with only the non-empty strings, preserving the original list −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> originalList = new List<string>() {
"Apple",
"",
"Banana",
" ",
"Cherry"
};
Console.WriteLine("Original list count: " + originalList.Count);
var filteredList = originalList.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
Console.WriteLine("Filtered list count: " + filteredList.Count);
Console.WriteLine("\nFiltered list contents:");
foreach (string item in filteredList) {
Console.WriteLine("'" + item + "'");
}
}
}
The output of the above code is −
Original list count: 5 Filtered list count: 3 Filtered list contents: 'Apple' 'Banana' 'Cherry'
Comparison of Methods
| Method | Modifies Original | Performance | Use Case |
|---|---|---|---|
| RemoveAll() | Yes | Fast | When you want to modify the original list |
| LINQ Where() | No | Moderate | When you need to preserve the original list |
| RemoveAt(index) | Yes | Slow for multiple items | When removing specific positions only |
Conclusion
To remove empty strings from a list in C#, use RemoveAll(string.IsNullOrEmpty) for truly empty strings or RemoveAll(x => string.IsNullOrWhiteSpace(x)) to also remove whitespace-only strings. For preserving the original list, use LINQ's Where() method to create a filtered copy.
