46

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 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

88
var mobileNos = number.Replace(" ", "")
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

9

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 Comment

Note that StringSplitOptions.TrimEntries is only available in .NET 5+
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);

Comments

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.

Comments

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

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

2 Comments

Please provide some explanation why do you think your proposed solution might help the OP.
@PeterCsala the answer is wrong but explains about as much as the other answers since the code speaks for itself.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.