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 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.


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 the splitWords Array.
    • The splitWords Array contain the elements as shown in the output follows.

When we run above program, then it generates below output.

Split_Function_Sample_1.0
Split_Function_Sample_1.0

Handling whitespace while using Split() function:

By default, String.Split() trims leading and trailing whitespace from the substrings it generates.

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.


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.

When we run above program, then it generates below output.

Split_Function_With_Space_Sample_2.0
Split_Function_With_Space_Sample_2.0

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.

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.

When we run above program, then it generates below output.

Split_Function_With_No_Space_Sample_3.0
Split_Function_With_No_Space_Sample_3.0

Splitting a string with multiple delimiters in C#:

You can split a string based on multiple characters by providing an array of delimiter strings:


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. The Split method splits the input string into substrings.
    • After splitting the string, the resulting array contains output as shown in the screenshot below.

When we run above program, then it generates below output.

Split_Function_With_Multiple_Characters_Sample_4.0
Split_Function_With_Multiple_Characters_Sample_4.0

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.

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#