String.Split()
will split on every single whitespace, so the result will contain empty strings usually. The Regex solution Ruben Farias has given is the correct way to do it. I have upvoted his answer but I want to give a small addition, dissecting the regex:
\s
is a character class that matches all whitespace characters.
In order to split the string correctly when it contains multiple whitespace characters between words, we need to add a quantifier (or repetition operator) to the specification to match all whitespace between words. The correct quantifier to use in this case is +
, meaning "one or more" occurrences of a given specification. While the syntax "\s+"
is sufficient here, I prefer the more explicit "[\s]+
".