Hit enter after type your search item

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(‘ ‘);

Where 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:


The output:

c# string-split

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 delimiter

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

See the code and output:


The output:

c# string-split-comma

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 as using the Split method of C#. This is accomplished by using the delimiters separated by commas.

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


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 require 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 telephone number contains spaces, it is still taken as one string. How can we do this in C#?

One of the ways is using the public string[] Split (char[] separator, int count); method. In that case, the count specifies the required number splits for the specified string.

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


The output of the code:

c# string split limit

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

This div height required for enabling the sticky sidebar