C# String.Split() – Examples

String.Split() method is used to split a string into a maximum number of substrings based on a specified delimiting character and, optionally, options. Splits a string into a maximum number of substrings based on the provided character separator, optionally omitting empty substrings from the result.

In this tutorial, we will learn about the syntax and examples for the following variants of C# String.Split() method based on the set of parameters.

  • String.Split(separator, count, options)
  • String.Split(separator, options)
  • String.Split(separators)
  • String.Split(separators, count)
  • String.Split(separators, count, options)
  • String.Split(separators, options)
  • String.Split(separator, count, options)
  • String.Split(separator, options)
  • String.Split(separators, count, options)
  • String.Split(separators, options)

Split(separator, count, options)

String.Split(separator, count, options) splits this string into a maximum of count number of substrings based on a specified delimiting character separator and, optionally, options.

Empty substrings would be emitted from the result.

Syntax

The syntax of Split() method with separator, count and options as parameters is

</>
Copy
String.Split(char separator, int count, StringSplitOptions options = System.StringSplitOptions.None)

where

ParameterDescription
separator[Mandatory] The delimiter character to split the given string.
count[Mandatory] The maximum number of elements expected in the resulting array.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 1 – Split(separator, count, options)

In this example, we will take a string and split it using separator character '-' into maximum of 3 splits with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c -  def-gh-ijk";
        Char separator = '-';
        Int32 count = 3;
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separator, count, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def-gh-ijk

Example 2 – Split(separator, count, options)

If the count is greater than the possible number of splits, then the resulting array consists of only the possible splits whose number is less than the given count.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c -  def-gh-ijk";
        Char separator = '-';
        Int32 count = 100;
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separator, count, options);
        Console.WriteLine(result);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Split(separator, options)

String.Split(separator) splits a string into substrings based on a specified delimiting character and, optionally, options.

Syntax

The syntax of Split() method with separator and options as parameters is

</>
Copy
String.Split(Char separator, StringSplitOptions options)

where

ParameterDescription
separator[Mandatory] The character that separates the substrings in this string.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 3 – Split(separator, options)

In this example, we will take a string and split it using separator character '-' with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c -  def-gh-ijk";
        Char separator = '-';
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separator, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Example 4 – Split(separator, options) – No options parameter

options parameter is optional. In the following program, we do not pass any argument for options parameter.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c -  def-gh-ijk";
        Char separator = '-';
        String[] result = str.Split(separator);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Split(separators)

String.Split(separators) splits a string into substrings based on specified delimiting characters in separators char array.

Syntax

The syntax of Split() method with char array of separators is

</>
Copy
String.Split(Char[] separators)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.

If separators is empty char array or null, then the split happens based on white space characters as delimiters.

Return Value

This method returns String Array.

Example 5 – Split(separators)

In this example, we will take a string and split it with two delimiters '-' and '*'.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c *  def*gh-ijk";
        Char[] separators = {'-', '*'};
        String[] result = str.Split(separators);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
   c
  def
gh
ijk

Example 6 – Split(separators) – separators is null

In this example, we will take a string and split it with two delimiters '-' and '*'.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-   c *  def*gh-ijk";
        Char[] separators = null;
        String[] result = str.Split(separators);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab-


c
*

def*gh-ijk

Split(separators, count)

String.Split(separators, count) splits a string into a maximum count number of substrings based on specified separators.

Syntax

The syntax of Split() method with separators char array and count as paramters is

</>
Copy
String.Split(Char[] separators, Int32 count)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.
count[Mandatory] The maximum number of substrings to return.

Return Value

This method returns String Array.

Example 7 – Split(separators, count)

In this example, we will

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-c*def*gh-ijk";
        Char[] separators = {'-', '*'};
        Int32 count = 4;
        String[] result = str.Split(separators, count);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh-ijk

Split(separators, count, options)

String.Split(separators, count, options) splits a string into a maximum count number of substrings based on specified delimiting characters separators and, optionally, options.

Syntax

The syntax of Split() method with separators, count and options as parameters is

</>
Copy
String.Split(Char[] separators, Int32 count, StringSplitOptions options)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.
count[Mandatory] The maximum number of substrings to return.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 8 – Split(separators, count, options)

In this example, we will take a string and split it using separator characters '-' and '*' into maximum of 3 splits with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-c*  def*gh-ijk";
        Char[] separators = {'-', '*'};
        Int32 count = 3;
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separators, count, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def*gh-ijk

Split(separators, options)

String.Split(separators, options) splits a string into substrings based on specified delimiting characters separators and options whether to remove empty entries or trim the substrings.

Syntax

The syntax of Split() method with separators char array and options as parameters is

</>
Copy
String.Split(Char[] separators, StringSplitOptions options)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 9 – Split(separators, options)

In this example, we will take a string and slit it based on multiple delimiting characters '-', '*' with option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab-c*  def*gh-ijk";
        Char[] separators = {'-', '*'};
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separators, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Split(separator, count, options)

String.Split(separator, count, options) splits a string into a maximum count number of substrings based on a specified delimiting separator string and, optionally, options.

Syntax

The syntax of Split() method with separator string, maximum count and options as parameters is

</>
Copy
String.Split(String separator, Int32 count, StringSplitOptions options)

where

ParameterDescription
separator[Mandatory] The delimiter string to split the given string.
count[Mandatory] The maximum number of elements expected in the resulting array.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 10 – Split(separator, count, options)

In this example, we will take a string and split it using separator string "mm" into maximum of 3 splits with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abmmcmmdefmmghmmijk";
        String separator = "mm";
        Int32 count = 3;
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separator, count, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
defmmghmmijk

Split(separator, options)

String.Split(separator, options) splits a string into substrings that are based on the provided string separator and optionally, options.

Syntax

The syntax of Split() method separator String and options as parameters is

</>
Copy
String.Split(String separator, StringSplitOptions options)

where

ParameterDescription
separator[Mandatory] The delimiter string to split the given string.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 11 – Split(separator, options)

In this example, we will take a string and split it using separator string "mm" with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "abmmcmm  defmmghmmijk";
        String separator = "mm";
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separator, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Split(separators, count, options)

String.Split(separator, count, options) splits a string into a maximum count number of substrings based on specified delimiting strings in the string array separators and, optionally, options.

Syntax

The syntax of Split() method with separators String Array, maximum number of splits, and options is

</>
Copy
String.Split(String[] separators, Int32 count, StringSplitOptions options)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.
count[Mandatory] The maximum number of elements expected in the resulting array.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 12 – Split(separators, count, options)

In this example, we will take a string and split it using separator strings "mm" and "--" into maximum of 4 splits with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab--cmm  defmmghmmijk";
        String[] separators = {"mm", "--"};
        Int32 count = 4;
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separators, count, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
ghmmijk

Split(separators, options)

String.Split(separators, options) splits a string into substrings based on a specified delimiting strings separators and, optionally, options.

Syntax

The syntax of Split() method with separators String array and options as parameters is

</>
Copy
String.Split(String[] separators, StringSplitOptions options)

where

ParameterDescription
separators[Mandatory] An array of delimiting characters.
options[Optional] Options that specify whether to trim substrings and include empty substrings. Possible values are:
1. System.StringSplitOptions.None
2. System.StringSplitOptions.TrimEntries
3. System.StringSplitOptions.RemoveEmptyEntries

Return Value

This method returns String Array.

Example 13 – Split(separators, options)

In this example, we will take a string and split it using separator strings "mm" and "--" with an option to trim substrings.

C# Program

</>
Copy
using System;
 
class Example {
    static void Main(string[] args) {
        String str = "ab--cmm  defmmghmmijk";
        String[] separators = {"mm", "--"};
        StringSplitOptions options = System.StringSplitOptions.TrimEntries;
        String[] result = str.Split(separators, options);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
c
def
gh
ijk

Conclusion

In this C# Tutorial, we have learnt the syntax of C# String.Split() method, and also learnt how to use this method with the help of C# example programs.