The C# String.Split() method 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.

Syntax of String.Split method:

 The basic syntax of the String.Split() method in C# 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 C# String.Split() method:

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”

Splitting a string while handling empty entries:

By default, the Split() method includes empty entries while splitting an input string. In some cases, you might need to exclude the empty entries from the resulting array, in that scenario 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:

We can also 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

Conclusion:

The C# String.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#