EN
C# / .NET - split string and remove empty substrings
3 points
In C# / .NET it is possible to split string and remove empty substrings in following way.
xxxxxxxxxx
1
string text = "John,Mary,,,Laura,Chris";
2
string[] array = text.Split(",", StringSplitOptions.RemoveEmptyEntries);
3
4
foreach (string entry in array)
5
Console.WriteLine(entry);
Output:
xxxxxxxxxx
1
John
2
Mary
3
Laura
4
Chris
xxxxxxxxxx
1
string text = "John++++Mary++Laura++++Chris";
2
string[] array = text.Split("++", StringSplitOptions.RemoveEmptyEntries);
3
4
foreach (string entry in array)
5
Console.WriteLine(entry);
Output:
xxxxxxxxxx
1
John
2
Mary
3
Laura
4
Chris
xxxxxxxxxx
1
string text = "John++Mary,,,,,Laura++Chris";
2
string[] array = text.Split(new char[] { ',', '+' }, StringSplitOptions.RemoveEmptyEntries);
3
4
foreach (string entry in array)
5
Console.WriteLine(entry);
Output:
xxxxxxxxxx
1
John
2
Mary
3
Laura
4
Chris
xxxxxxxxxx
1
string text = "John[+]Mary,,,Laura[+]Chris,,[+][+]";
2
string[] array = text.Split(new string[] { ",", "[+]" }, StringSplitOptions.RemoveEmptyEntries);
3
4
foreach (string entry in array)
5
Console.WriteLine(entry);
Output:
xxxxxxxxxx
1
John
2
Mary
3
Laura
4
Chris