C# String Split

Summary: in this tutorial, you will learn how to use the C# String Split a string into an array of substrings based on a specified delimiter or set of delimiters.

Introduction to the C# String Split() method

The String Split() method splits a string into an array of substrings based on a specified delimiter or a set of delimiters.

The Split() method has some overloads that allow you to customize the splitting operations.

The following shows the basic syntax of the Split() method:

public string[] Split (
    char separator, 
    StringSplitOptions options = System.StringSplitOptions.None
);Code language: C# (cs)

In this syntax:

  • separator is a character that delimits the substrings in the current string.
  • options parameter allows you to trim the substring or include empty substrings by combining the bitwise enumeration values.

The StringSplitOptions has the following members:

  • None – default option when splitting strings.
  • RemoveEmptyEntries – omit array elements that contain empty strings from the result.
  • TrimEntries – trim the white-space characters from each substring in the result.

The Split() method returns an array whose elements contain the substrings.

The following example shows how to use the Split() method to split a string by a comma:

using static System.Console;

var input = "1,2,3";
var numbers = input.Split(',');
foreach (var number in numbers)
{
    WriteLine(number);
}Code language: C# (cs)

Output:

1
2
3Code language: C# (cs)

Note that 1, 2, and 3 are substrings, not numbers. To convert them to integers, you need to use the int.Parse() method:

using static System.Console;

var input = "1,2,3";
var numbers = input.Split(',');
foreach (var number in numbers)
{
    WriteLine(int.Parse(number));
}Code language: C# (cs)

If the input has a space after each comma, the result substring will include the space. For example:

using static System.Console;

var input = "1, 2, 3";
var numbers = input.Split(',');
foreach (var number in numbers)
{
    WriteLine(number);
}Code language: C# (cs)

Output:

1
 2
 3Code language: C# (cs)

To remove the space while splitting the string, you can use the StringSplitOptions argument like this:

using static System.Console;

var input = "1, 2, 3";
var numbers = input.Split(
    ',', 
    StringSplitOptions.TrimEntries
);

foreach (var number in numbers)
{
    WriteLine(number);
}Code language: C# (cs)

Output:

1
2
3Code language: C# (cs)

If the input has an empty string, you can use the RemoveEmptyEntries option to exclude it from the resulting array:

using static System.Console;

var input = "1, 2, , 3";
var numbers = input.Split(
    ',', 
    StringSplitOptions.TrimEntries | 
    StringSplitOptions.RemoveEmptyEntries
);

foreach (var number in numbers)
{
    WriteLine(number);
}Code language: C# (cs)

Output:

1
2
3Code language: C# (cs)

Using C# String Split() to split a string by multiple delimiters

The Split() method has an overload that allows you to split a string by multiple delimiters:

public string[] Split (
    char[]? separator, 
    StringSplitOptions options
);Code language: C# (cs)

In this syntax, the separator is an array of delimiters that the Split() method uses to split the current string. For example:

using static System.Console;

var input = "1;2,3:4";

char[] delimiters = { ',', ';', ':' };
var numbers = input.Split(delimiters);

foreach (var number in numbers)
{
    WriteLine(number);
}Code language: C# (cs)

Output:

1
2
3
4Code language: C# (cs)

Using C# String Split() to split a string and limit the number of substrings

Sometimes, you want to limit the maximum number of substrings when splitting a string. To do that, you use the following overload of the Split() method:

public string[] Split (
   char[]? separator, 
   int count, 
   StringSplitOptions options
);Code language: C# (cs)

In this syntax, the count specifies the maximum number of substrings to return. For example:

using static System.Console;

var input = "12:30 PM";

char[] delimiters = { ':',' ' };
var parts = input.Split(delimiters, 3);

foreach (var part in parts)
{
    WriteLine(part);
}Code language: C# (cs)

Output:

12
30
PMCode language: C# (cs)

In this example, we use the Split() method to split a time string by characters : and space, and limit the number of returned substrings to 3.

Other C# String Split() overloads

Besides accepting a character or an array of characters as delimiters, the Split() allows you to pass a string or an array of strings as delimiters:

public string[] Split (
   string[]? separator, 
   StringSplitOptions options
);

public string[] Split (
   string? separator, 
    int count, 
    StringSplitOptions options = System.StringSplitOptions.None
);Code language: C# (cs)

Summary

  • Use the C# String Split() method to split a string by one or more delimiters and returns an array of substrings.
Was this tutorial helpful ?