C# String Split() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, Split() cuts a string into pieces wherever a separator appears and returns them as a string[] (an array). The separators themselves are not included in the pieces.

The separator can be one character (','), several characters (new[] { ' ', '\t' }), or a whole string (", "). Options let you drop empty pieces and trim spaces. The original string is not changed; you get brand-new strings in the array. The opposite operation is string.Join().

This is an instance member of System.String. Call it on a string variable, such as text.Split(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Split() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.

Syntax

Pass the separator, and optionally a StringSplitOptions value.

C#
string csv = "red,green,blue";
string[] parts = csv.Split(',');            // ["red", "green", "blue"]

string messy = "a, b,,c";
string[] clean = messy.Split(',',
    StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);   // ["a", "b", "c"]
CallWhat it does
text.Split(',')Split at every comma.
text.Split(new[] { ' ', '\t' })Split at any of several characters.
text.Split(", ")Split at a whole string (newer .NET, .NET Core 2.0 and later).
text.Split(',', StringSplitOptions.RemoveEmptyEntries)Drop empty pieces such as the one between ,,.
text.Split(':', 2)Return at most 2 pieces; the last piece keeps the rest of the text.

StringSplitOptions.TrimEntries removes spaces around each piece and is available in .NET 5 and later. Combine options with |. Calling Split() with no arguments splits at white-space characters. If no separator appears, the result is an array with one element: the whole string.

Library declarations
C#
public string[] Split(params char[]? separator);
public string[] Split(char separator, StringSplitOptions options = StringSplitOptions.None);              // newer .NET
public string[] Split(char[]? separator, int count);
public string[] Split(char[]? separator, StringSplitOptions options);
public string[] Split(string? separator, StringSplitOptions options = StringSplitOptions.None);           // newer .NET
public string[] Split(string? separator, int count, StringSplitOptions options = StringSplitOptions.None);   // newer .NET
public string[] Split(string[]? separator, StringSplitOptions options);

Parameters

ParameterDescription
separatorThe character(s) or string(s) to split at. null or empty means “white space”.
countOptional. The most pieces to return. The last piece holds whatever is left.
optionsOptional. None, RemoveEmptyEntries, TrimEntries (.NET 5+), or a combination.

Return Value

Return ValueDescription
string[]The pieces, in their original order. Never null; at least one element unless empty entries are removed.

C# Examples

Example 1 — Split a Comma-Separated List

Break a simple list into an array, print how many pieces there are, and loop over them.

C#
using System;

class Program {
    static void Main() {
        string csv = "red,green,blue";
        string[] parts = csv.Split(',');

        Console.WriteLine(parts.Length);
        foreach (string p in parts) {
            Console.WriteLine(p);
        }
    }
}

How It Works

  1. Two commas, three pieces. Every comma is a cut, so there is always one more piece than separators.
  2. Separators disappear. None of the pieces contains a comma.
  3. An ordinary array. parts.Length is 3, and parts[0] would be red.
  4. Order is kept. The foreach prints the colors left to right, just as they appear in csv.

Example 2 — Remove Empty Pieces and Trim Spaces

Split messy input with and without options. The brackets show exactly what each piece holds.

C#
using System;

class Program {
    static void Main() {
        string messy = "a, b,,c ";

        string[] raw = messy.Split(',');
        Console.WriteLine(raw.Length);
        Console.WriteLine("[" + string.Join("][", raw) + "]");

        string[] clean = messy.Split(',',
            StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        Console.WriteLine("[" + string.Join("][", clean) + "]");
    }
}

How It Works

  1. Raw pieces keep everything. Three commas make four pieces, including the empty one between ,,, a leading space on b, and a trailing space on c.
  2. Make pieces visible. string.Join("][", raw) glues the pieces with ][, and the outer brackets complete the frame, so empty text and spaces are easy to see.
  3. Two options at once. The | combines them: TrimEntries removes the spaces, and RemoveEmptyEntries drops the empty piece.
  4. Version note. TrimEntries needs .NET 5 or later. On older versions, call Trim() on each piece yourself.

Example 3 — Split at a String, with a Piece Limit

Split a header line at the first ": " only, then split words at spaces or tabs.

C#
using System;

class Program {
    static void Main() {
        string line = "key: value: with colon";
        string[] kv = line.Split(": ", 2);
        Console.WriteLine(kv[0]);
        Console.WriteLine(kv[1]);

        string words = "one two\tthree";
        Console.WriteLine(words.Split(new[] { ' ', '\t' }).Length);
    }
}

How It Works

  1. A string separator. ": " is two characters; the split happens only where both appear together.
  2. At most two pieces. The 2 stops after the first cut, so the second piece keeps its own ": " untouched. This is perfect for key and value lines.
  3. Several single separators. A char[] with a space and a tab cuts at either one, giving three words.
  4. Version note. The overload that takes one string separator and a count is in newer .NET (.NET Core 2.0 and later). On .NET Framework, pass new[] { ": " } as a string[] with a count and options.

Frequently Asked Questions

A string array with the pieces between separators, in order. The separators are not included. If no separator is found, the array has one element: the whole string.
Pass StringSplitOptions.RemoveEmptyEntries, for example text.Split(',', StringSplitOptions.RemoveEmptyEntries).
In newer .NET, call text.Split(", "). On .NET Framework, use text.Split(new[] { ", " }, StringSplitOptions.None).
It trims white space from the start and end of each piece. It is available in .NET 5 and later, and you can combine it with RemoveEmptyEntries using |.
Use string.Join(separator, pieces). It is the opposite of Split.

Next: StartsWith()

Check whether a string begins with a given prefix.

StartsWith() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful