This article illustrates the different techniques to split a string on the whitespace characters in C#.

The standard solution to split a string on the given characters is using the String.Split() method. If no delimiter is specified, it splits the string on whitespace characters. Here’s an example of its usage:

Download  Run Code

 
To handle multiple spaces, you can use the String.Split(char[]) method overload with StringSplitOptions.RemoveEmptyEntries option, as shown below.

Download  Run Code

 
The above solution creates a new object. To avoid creating a new object, you can use (char[]) null instead of new char[0].

Download  Run Code

 
You can also create an extension function to split a string on whitespace. To split the string only on some whitespace characters, you can pass them in the char array.

Download  Run Code

That’s all about splitting a string on the whitespace characters in C#.