How to use split to filter out multiple empty strings in C#?
You can use the StringSplitOptions.RemoveEmptyEntries parameter to filter out multiple empty strings. Here is an example code showing how to filter out multiple empty strings using the split method:
string input = "Hello,,World,,,!";
char[] separators = new char[] { ',' };
string[] result = input.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in result)
{
Console.WriteLine(word);
}
The output is:
Hello
World
!
In the above example, we used a comma as a separator and passed the StringSplitOptions.RemoveEmptyEntries parameter to the Split method. This way, consecutive empty strings will be filtered out and only non-empty substrings will be returned.