44

I want to remove empty and null string in the split operation:

 string number = "9811456789,   ";
 List<string> mobileNos = number.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(mobile => mobile.Trim()).ToList();

I tried this but this is not removing the empty space entry

4
  • Before selecting clean up the list with .Where(x => !x.IsNullOrEmpty()).Select( ...) Commented Sep 28, 2017 at 10:49
  • 1
    but its not empty it has spaces in .. you could add a where clause to remove them
    – BugFinder
    Commented Sep 28, 2017 at 10:49
  • 2
    @AntonSizikov should be !x.IsNullOrWhiteSpace().... Commented Sep 28, 2017 at 10:51
  • Yup, it should. Commented Sep 28, 2017 at 10:52

7 Answers 7

81
var mobileNos = number.Replace(" ", "")
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
16

As I understand it can help to you;

string number = "9811456789, ";
List<string> mobileNos = number.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

the result only one element in list as [0] = "9811456789".

Hope it helps to you.

8

The easiest and best solution is to use both StringSplitOptions.TrimEntries to trim the results and StringSplitOptions.RemoveEmptyEntries to remove empty entries, fed in through the pipe operator (|).

string number = "9811456789,   ";
List<string> mobileNos = number
    .Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
    .ToList();

Checkout the below test results to compare how each option works,

enter image description here

1
  • 1
    Note that StringSplitOptions.TrimEntries is only available in .NET 5+ Commented Nov 29, 2022 at 10:16
6

a string extension can do this in neat way as below the extension :

        public static IEnumerable<string> SplitAndTrim(this string value, params char[] separators)
        {
            Ensure.Argument.NotNull(value, "source");
            return value.Trim().Split(separators, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
        }

then you can use it with any string as below

 char[] separator = { ' ', '-' };
 var mobileNos = number.SplitAndTrim(separator);
5

I know it's an old question, but the following works just fine:

string number = "9811456789,   ";
List<string> mobileNos = number.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();

No need for extension methods or whatsoever.

3
"string,,,,string2".Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

return ["string"],["string2"]

2
  • 1
    Please provide some explanation why do you think your proposed solution might help the OP. Commented Sep 21, 2020 at 7:11
  • @PeterCsala the answer is wrong but explains about as much as the other answers since the code speaks for itself. Commented Sep 21, 2020 at 7:33
1

If you are using Linq. This work for me.

string temp = "a,b,   ,c,  ";    
List<string> result = temp.Split(',').Where(i => string.IsNullOrWhiteSpace(i) == false).ToList();

Not the answer you're looking for? Browse other questions tagged or ask your own question.