HOWTO · Csharp

Split a String in C# with String.Split()

Split C# strings with character or string delimiters, handle empty entries, and compare String.Split() with Java StringTokenizer.

On this page

In C#, string.Split() breaks a string into an array of substrings at the delimiter values you provide. Use a character delimiter for a simple separator, an array of characters for several alternatives, or an array of strings for multi-character separators. The method does not interpret delimiters as regular expressions. The four complete programs below demonstrate these forms and show the corresponding runtime output.

Understanding string.Split()

String.Split() returns the pieces before, between, and after matching delimiters as elements of a string[]. The separators are removed from the returned elements. Character and string delimiters are literal values, not patterns. Use StringSplitOptions.RemoveEmptyEntries when you want to discard zero-length elements created by adjacent or outer separators.

Tokenizing with a Single Delimiter

For a string with words separated by spaces, pass the space character to Split. This complete program writes each resulting token on its own line.

Run this example:

using System;

class Program
{
    static void Main()
    {
        string input = "Hello world this is C# programming";
        string[] tokens = input.Split(' ');

        foreach (string token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}
Hello
world
this
is
C#
programming

The returned array contains the six words in their original order. If the input has repeated spaces, the default behavior can include zero-length elements between them; see the option below when those elements should be omitted.

Tokenizing with Multiple Delimiters

Pass a char[] to split at any of several single-character separators. Each listed character is an independent delimiter, so commas, semicolons, and colons all divide the input in the same way.

using System;

class Program
{
    static void Main()
    {
        string input = "apple,banana;orange:grape";
        char[] delimiters = { ',', ';', ':' };
        string[] tokens = input.Split(delimiters);

        foreach (string token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}
apple
banana
orange
grape

This produces the four fruit names as separate array elements. The delimiter array means “split on any listed character”; it does not require the characters to occur together.

Removing Empty Entries

A delimiter at the start or end of a string, or two delimiters next to each other, can create a zero-length element. For example, splitting "red,,blue," on a comma retains the gap between the consecutive commas and the trailing gap by default. Add StringSplitOptions.RemoveEmptyEntries to keep only nonempty tokens:

using System;

class Program
{
    static void Main()
    {
        string input = "red,,blue,";
        string[] tokens = input.Split(
            new[] { ',' },
            StringSplitOptions.RemoveEmptyEntries);

        foreach (string token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}
red
blue

This option filters the resulting zero-length elements; it does not trim whitespace from the remaining tokens. If spaces around values should also be removed, trim each token explicitly after splitting.

Using String Delimiters

Use a string array when each separator has multiple characters or is a word. Each separator is matched as a whole literal string. This differs from a regular expression, where symbols can represent a pattern.

using System;

class Program
{
    static void Main()
    {
        string input = "north--east::south";
        string[] delimiters = { "--", "::" };
        string[] tokens = input.Split(delimiters, StringSplitOptions.None);

        foreach (string token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}
north
east
south

The array contains the three direction names, with both characters of each matched separator removed. To ignore zero-length results in this version, replace StringSplitOptions.None with StringSplitOptions.RemoveEmptyEntries.

Choosing Between string.Split() and StringTokenizer

C# string.Split() is a convenient choice when the result should be an array and the separators should be discarded. Character arrays handle several one-character alternatives; string arrays handle literal multi-character separators. Choose the StringSplitOptions value based on whether zero-length elements should remain.

Java’s StringTokenizer can also accept multiple delimiter characters in its delimiter argument, so it is inaccurate to describe it as limited to one delimiter. By default, it returns tokens and discards delimiters; its returnDelims option can make delimiters tokens too. Use StringTokenizer when working with Java code that needs its token-by-token API, and use string.Split() for the direct C# array-based operation. Neither API treats its delimiter argument as a regular expression.