Home > Mobile >  c# String.Split((string)null, , StringSplitOptions.RemoveEmptyEntries) doesn't split on whitesp
c# String.Split((string)null, , StringSplitOptions.RemoveEmptyEntries) doesn't split on whitesp

Time:01-06

I had code (in this case targeting .NET Core 3.1) where I wanted to split a string on whitespace. My draft code was

string s = "A B";
var parts = s.Split(null, StringSplitOptions.RemoveEmptyEntries);

but the compiler complained about ambiguity between the first argument being a (char[]) or a (string?). I chose to cast the null to a string:

string s = "A B";
var parts = s.Split((string)null, StringSplitOptions.RemoveEmptyEntries);

However, the result was unexpectedly an array of one string "A B".

As it turns out, plodding through the repetitive documentation for all the overloads of Split reveals that this signature, the one that takes a single string delimiter, is the only one where a null delimiter does not mean to break on whitespace.

Had I coded (string[])null, (char[])null, new char[0], or new string[0] I would have obtained the results I expected. I must admit that the incomplete compiler error, which failed to list all the ambiguous possibilities, somewhat led me down this wrong path. I had string delimiters on the brain and so discounted the (char[]) version from the error message and went with the (string) one.

But that leaves the question, what does Split((string)null, ...) actually do? The documentation doesn't seem to say either way, but apparently the answer is that it just returns the original string in a one-element array.

CodePudding user response:

Taken directly from the source code for .Net Standard 2.1, which includes .Net Core 3.1, the definition of Split(string? separator, StringSplitOptions options = StringSplitOptions.None), at source.dot.net:

public string[] Split(string? separator, StringSplitOptions options = StringSplitOptions.None)
{
    return SplitInternal(separator ?? string.Empty, null, int.MaxValue, options);
}

As you can see, it checks for null and passes string.Empty to the method SplitInternal, so to answer your question, it splits on an empty string, which produces the original string.

These produce the same results:

string s = "Hello World";   
Console.WriteLine(s.Split((string)null, StringSplitOptions.RemoveEmptyEntries)[0]); //prints Hello World
Console.WriteLine(s.Split(string.Empty, StringSplitOptions.RemoveEmptyEntries)[0]); //prints Hello World
  • Related