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.
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
Parameter
Description
separator
The character(s) or string(s) to split at. null or empty means “white space”.
count
Optional. The most pieces to return. The last piece holds whatever is left.
options
Optional. None, RemoveEmptyEntries, TrimEntries (.NET 5+), or a combination.
Return Value
Return Value
Description
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);
}
}
}
Output
3
red
green
blue
How It Works
Two commas, three pieces. Every comma is a cut, so there is always one more piece than separators.
Separators disappear. None of the pieces contains a comma.
An ordinary array.parts.Length is 3, and parts[0] would be red.
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.
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.
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.
Two options at once. The | combines them: TrimEntries removes the spaces, and RemoveEmptyEntries drops the empty piece.
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);
}
}
Output
key
value: with colon
3
How It Works
A string separator.": " is two characters; the split happens only where both appear together.
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.
Several single separators. A char[] with a space and a tab cuts at either one, giving three words.
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.