ZetCode

C# split string

last modified July 5, 2020

C# split string tutorial shows how to split strings in C# language. C# tutorial is a comprehensive tutorial on 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.

C# split string simple example

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

Program.cs
using System;

namespace Simple
{
    class Program
    {
        static void Main(string[] args)
        {
            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

This is the output.

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;

namespace OmitEmpty
{
    class Program
    {
        static void Main(string[] args)
        {
            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;

namespace MultipleSeparators
{
    class Program
    {
        static void Main(string[] args)
        {
            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;

namespace LimitSplits
{
    class Program
    {
        static void Main(string[] args)
        {
            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;

namespace SplitByString
{
    class Program
    {
        static void Main(string[] args)
        {
            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

This is the output.

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;

namespace SplitRegex
{
    class Program
    {
        static void Main(string[] args)
        {
            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;

namespace JoinString
{
    class Program
    {
        static void Main(string[] args)
        {
            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

This is the output.

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

Read C# tutorial or list all C# tutorials.