Splitting a string at all whitespace

String.Split() (no parameters) does split on all whitespace (including LF/CR)


If you want to avoid regex, you can do it like this:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit"
    .Split()
    .Where(x => x != string.Empty)

Visual Basic equivalent:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit" _
    .Split() _
    .Where(Function(X$) X <> String.Empty)

The Where() is important since, if your string has multiple white space characters next to each other, it removes the empty strings that will result from the Split().

At the time of writing, the currently accepted answer (https://stackoverflow.com/a/1563000/49241) does not take this into account.


Try this:

Regex.Split("your string here", "\s+")

Tags:

.Net

Vb.Net