C# String Split method

C# String Split method explained with examples and code

What is C# String Split method?

In C#, a string can be broken by one or more given delimiters by using the Split method. The simple way of using the Split method can be:

Source_string.Split(‘ ‘);

There:

  • Source_string is the string that you want to break.
  • The delimiter like a comma, space, etc. is specified after the Split in parenthesis.
  • The Split method returns an array of broken strings.

You may also specify more than one delimiters as follows:

Source_string.Split(‘ ‘, ‘,’, ‘:’);

That is, just separate the delimiters by a comma. The section below shows you using the C# Split method with variations. I will also show you how to iterate through the returned array by using the foreach statement; so keep reading.

The example of String split by space

In this example, I will break the given string by space delimiter. The returned array after executing the Split method is used in the foreach statement, so the array elements are displayed:

using System;

class Split_string_demo

{
    static void Main()
    {
        string str_split = "The Demos of Splitting Strings!";
        string[] broken_str = str_split.Split(' ');

        foreach (var sub_str in broken_str)
        {
            System.Console.WriteLine(sub_str);
        }
        Console.ReadLine();
    }
}

The output:

The
Demos
of
Splitting
Strings!

You can see, the returned array after using the Split method is assigned to a string array (broken_str).

This array is used in the foreach statement where its elements are displayed.

The example of using comma as a delimiter

In this example, we have a string that contains spaces and commas. However, in the Split method, I specified the comma as a delimiter where the given string words should be broken.

See the code and output:

using System;

class Split_string_comma_demo

{
    static void Main()
    {
        string str_split_comma = "Fruits & Vegetables, Animal and Birds, Earth and Sky";
        string[] broken_str_comma = str_split_comma.Split(',');

        foreach (var sub_str_comma in broken_str_comma)
        {
            System.Console.WriteLine(sub_str_comma);
        }
        Console.ReadLine();
    }
}

The output:

Fruits & Vegetables
Animal and Birds
Earth and Sky
You may notice the broken substrings also display a space which means the source string is broken where a comma is found.

Using the comma is particularly useful if you have to deal with the CSV files (Comma Separated Values).

Using multiple delimiters example

As mentioned earlier, you may use more than one delimiters for splitting a string by using the Split method of C#.

This is accomplished by using the delimiters separated by commas.

In the example below, we have used a comma and space and see how the given string is broken:

using System;

class Split_string_multiple_delimiters

{
    static void Main()
    {
        string str_split_multiple = "Black & White, Yellow and Green, Blue and White";
        string[] broken_str_multiple = str_split_multiple.Split(',', ' ');

        foreach (var sub_str_multiple in broken_str_multiple)
        {
            System.Console.WriteLine(sub_str_multiple);
        }
        Console.ReadLine();
    }
}

The output:

c# string split multiple

You can see in the output snapshot, the string is broken from comma and space characters.

How to limit the number of splits of a string?

In certain scenarios, you may only be required to split the string from the first one, two or so occurrences of the specified delimiter(s). For example, if we have this string:

“My Telephone 123 456 4757”

And we want to break it like this:

My

Telephone

123 456 4757

Although a telephone number contains spaces, it is still taken as one string. How can we do this in C#?

One of the ways is using:

public string[] Split (char[] separator, int count);

In that case, the count specifies the required number of splits for the specified string.

See the demo below for the same string as used above:

using System;

class Split_string_limit
{
    static void Main()
    {
        string str_split_limit = "My Telephone 123 456 4757";
        string[] broken_str_limit = str_split_limit.Split(new char[] { ' ' }, 3);

        foreach (var sub_str_limit in broken_str_limit)
        {
           System.Console.WriteLine(sub_str_limit);
        }
        Console.ReadLine();
    }
}

The output of the code:

My
Telephone
123 456 4757

You can see, as I specified the space delimiters with a count of 2, the first two occurrences in the specified string broke and we got the last part with space unbroken.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!