ZetCode

C# split string

last modified March 6, 2021

C# split string tutorial shows how to split strings in C# language.

C# String.Split

The String.Split method creates an array of substrings by splitting the input string based on one or more delimiters. Strings can be split with the Regex.Split method as well; it splits a string based on a regular expression pattern.

Split(Char[], Int32, StringSplitOptions)
Split(Char, Int32, StringSplitOptions)
Split(String[], Int32, StringSplitOptions)
Split(String[], StringSplitOptions)
Split(String, Int32, StringSplitOptions)
Split(Char[], StringSplitOptions)
Split(Char[], Int32)
Split(Char, StringSplitOptions)
Split(String, StringSplitOptions)
Split(Char[])

There are ten overloaded String.Split methods.

Split(String, String, RegexOptions, TimeSpan)
Split(String, String, RegexOptions)
Split(String, Int32, Int32)
Split(String, Int32)
Split(String)
Split(String, String)

There are seven overloaded Regex.Split methods.

C# split string simple example

The following is a simple demonstration of the String.Split method.

Program.cs
using System;

var text = "There is an old hawk in the sky";

var words = text.Split(' ');
Array.ForEach(words, Console.WriteLine);

In the example, we split a sentence into an array of words by a space character. With the Array.ForEach method we print all the words to the terminal.

$ dotnet run
There
is
an
old
hawk
in
the
sky

C# split string and omit empty entries

Splitting strings may result in empty entries. To avoid this, we can use the StringSplitOptions.RemoveEmptyEntries option.

Program.cs
using System;

var text = "There  is an old  hawk in the sky";

var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
Array.ForEach(words, Console.WriteLine);

In the example, we have multiple spaces between words. This would result in empty entries in the resulting array. We use the StringSplitOptions.RemoveEmptyEntries option to omit such empty entries.

C# split string by multiple characters

C# allows to split a string by using multiple separators.

Program.cs
using System;

var text = "falcon;eagle,forest,sky;cloud,water,rock;wind";

var words = text.Split(new char[] {',', ';'});
Array.ForEach(words, Console.WriteLine);

In the example, we split the string by using two characters: comma and semicolon.

var words = text.Split(new char[] {',', ';'});

The overloaded String.Split method takes an array of characters as a parameter.

Limiting the number of substrings

We can limit the number of substrings with an overloaded method that takes the second count parameter.

Program.cs
using System;

var text = "falcon,eagle,forest,sky,cloud,water,rock,wind";

var words = text.Split(',', 4);
Array.ForEach(words, Console.WriteLine);

In the example, we split the string into four substrings.

$ dotnet run
falcon
eagle
forest
sky,cloud,water,rock,wind

The last string contains all the remaining text.

C# split string by string

Sometimes, we might want to split strings by another string, not a character. There is an overloaded method for this.

Program.cs
using System;

var text = "hawkxxowlxxvulturexxeagle";

var birds = text.Split("xx");
Array.ForEach(birds, Console.WriteLine);

In the example, we split the string by "xx" string.

$ dotnet run
hawk
owl
vulture
eagle

C# split string with Regex.Split

The Regex.Split method splits an input string into an array of substrings at the positions defined by a regular expression match. C# Regular Expressions tutorial covers regular expressions in C# language in more detail.

Program.cs
using System;
using System.IO;
using System.Text.RegularExpressions;

var text = "There are\t\t many clouds   in the \n sky.";

var rx = new Regex(@"\s+", RegexOptions.Compiled);
var data = rx.Split(text);

Array.ForEach(data, Console.WriteLine);

The example splits a sentence into words by spaces.

var rx = new Regex(@"\s+", RegexOptions.Compiled);

The regular expression splits the string by one or more white spaces.

C# String.Join

In the previous examples, we have split strings into an array of substrings. The String.Join method concatenates all the elements of a string array, using the specified separator between each element.

Program.cs
using System;
using System.Collections.Generic;

var words = new List<string> {"falcon", "wood", "sky", "water"};

var text = String.Join(',', words);
Console.WriteLine(text);

In the example, we join a list of words by comma character.

$ dotnet run
falcon,wood,sky,water

In this tutorial we have split strings in C# language.

Read C# tutorial or list all C# tutorials.