The Split() method in C# is used to manipulate string variable, it allows to split a string into multiple parts based on a specified delimiter. In this blog, we’ll explore the String.Split()
method in detail.
String.Split() method in C#:
The String.Split()
method in C# splits an input string into an Array of substrings based on a specified delimiter or set of delimiters.
Basic Syntax and parameter of String.Split(): The basic syntax of the Split()
method is as follows:
string[] Split(char[] separator, StringSplitOptions options)
Here, the Split method takes the parameter as follows.
-
- char[] : Array of characters that act as delimiters.
- options: This parameter is used to specify how the Split method should handle empty entries in the resulting array.
- It is of type StringSplitOptions and can take one of the following values:
- None: This is the default option. It includes empty entries in the resulting array.
- RemoveEmptyEntries: This option excludes empty entries from the resulting array.
- It is of type StringSplitOptions and can take one of the following values:
- It returns an array of substring values as output.
Splitting a string by a Single delimiter in C# :
Let’s take an example to see how it works when splitting a string by a single delimiter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace SplitFunctionDemo { class Program { static void Main(string[] args) { string inputStr = "Welcome to TheDotnetGuide Website."; //Split the sentence using space character ' ' as the delimiter string[] splitWords = inputStr.Split(' '); //Print the splited word to the console foreach (string str in splitWords) { Console.WriteLine(str); } } } } |
In this example,
-
- We used the space character
' '
as the delimiter to split the input string. - The
Split()
method divides the sentence into words and stores them in thesplitWords
Array. - The
splitWords
Array contain the elements as shown in the output follows.
- We used the space character
When we run above program, then it generates below output.

Handling whitespace while using Split() function:
By default, String.Split()
trims leading and trailing whitespace from the substrings it generates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace SplitFunctionDemo { class Program { static void Main(string[] args) { string inputString = " Tiger , Elephant , Monkey, Cat "; // Define an array of delimiters char[] chrDelimiters = { ','}; //Split the sentence using space character ' ' as the delimiter string[] splitWords = inputString.Split(chrDelimiters); //Print the splited word to the console foreach (string str in splitWords) { Console.WriteLine(str); } } } } |
In this example,
- We have input string as ” Tiger , Elephant , Monkey, Cat “, that has leading and trailing spaces.
- After splitting the input string, the resulting array will contain substrings without any leading or trailing spaces.
- The output would be as follows.
- “Tiger”
- “Elephant “
- “Monkey”
- “Cat”
Spliting a string while handling empty entries in C#:
By default, the Split()
method includes empty entries, to exclude the empty entries from the resulting array, we can use the StringSplitOptions.RemoveEmptyEntries option.
Let’s take below example, where we use comma ','
to split the input string with the default behavior.
Example 1: Use of Split() function without StringSplitOptions.RemoveEmptyEntries option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace SplitFunctionDemo { class Program { static void Main(string[] args) { string inputStr = "Alex,Sachin,,Brett,,,Jason"; //Split the sentence using space character ',' as the delimiter string[] splitWords = inputStr.Split(','); //Print the splited word to the console foreach (string str in splitWords) { Console.WriteLine(str); } } } } |
In this example,
-
- The
Split
method splits the input string into an array using a comma','
as the delimiter. It includes the empty entry also because of the default behavior. - After splitting the string, the resulting array contains substrings with empty values as shown in the screenshot below.
- The
When we run above program, then it generates below output.

Let’s take an example, where we use comma ','
to split the input string with StringSplitOptions.RemoveEmptyEntries option.
Example 2: Use of Split() function using StringSplitOptions.RemoveEmptyEntries option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; namespace SplitFunctionDemo { class Program { static void Main(string[] args) { string inputStr = "Alex,Sachin,,Brett,,,Jason"; //Split the sentence using space character ' ' as the delimiter string[] splitWords = inputStr.Split(new char[] { ',' } , StringSplitOptions.RemoveEmptyEntries); //Print the splited word to the console foreach (string str in splitWords) { Console.WriteLine(str); } } } } |
In this example,
-
- The
Split
method splits the input string into an array using a comma','
as the delimiter. - We are using StringSplitOptions.RemoveEmptyEntries option, this removes the empty space entries.
- After splitting the string, the resulting array contains substrings without any empty spaces as shown in the screenshot below.
- The
When we run above program, then it generates below output.

Splitting a string with multiple delimiters in C#:
You can split a string based on multiple characters by providing an array of delimiter strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace SplitFunctionDemo { class Program { static void Main(string[] args) { string inputStr = "Mango|Orange,Banana|Grape|Papaya"; // Define an array of delimiters string[] arrDelimiters = { ",","|" }; //Split the sentence using space character ' ' as the delimiter string[] splitWords = inputStr.Split(arrDelimiters, StringSplitOptions.None); //Print the splited word to the console foreach (string str in splitWords) { Console.WriteLine(str); } } } } |
In this example,
-
- The input string contains a mix of comma
','
and pipes'|'
- We define an array of delimiters as arrDelimiters, containing comma
','
and pipes'|'
- Then, we use the
Split
method with this array as the separator. TheSplit
method splits the input string into substrings. - After splitting the string, the resulting array contains output as shown in the screenshot below.
- The input string contains a mix of comma
When we run above program, then it generates below output.

Performance Considerations for Split() in C#:
-
- The performance of the
Split()
method may vary based on the delimiter, if you are using a simple, single character delimiter (e.g. Comma or a space or pipe etc) then it performs better than a complex delimiters like regular expressions. - Splitting a short string is generally faster than splitting a long one. The time complexity of the
Split()
method is O(N), where N is the length of the input string.
- The performance of the
Conclusion:
The Split()
function is a handy tool for working with strings in C#, it allows you to extract meaningful data from text and perform various string manipulation tasks efficiently.
Thanks for visiting 🙂 please look at some of other blogs on similar topics Usage of String.Format() Method in C#