C# Reference ยท Strings and Text
C# String.Split() method
Reference for C# String.Split(): split by char or string, remove empty entries, trim, limit the count, edge cases and allocation-free alternatives.
Split cuts a string into an array of substrings at every occurrence of a separator. The separators themselves are not part of the result. You can split on a single character, on several characters, or on a whole string, and you can ask Split to drop empty pieces and trim whitespace.
It is the standard tool for simple formats such as CSV-like lines, key-value pairs and path segments. It does not understand quotes or escapes, so real CSV needs a parser.
string[] parts = "red,green,blue".Split(','); Console.WriteLine(parts.Length); // 3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public string[] Split(char separator, StringSplitOptions options = StringSplitOptions.None) public string[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None) public string[] Split(params char[]? separator) public string[] Split(char[]? separator, StringSplitOptions options) public string[] Split(string? separator, StringSplitOptions options = StringSplitOptions.None) public string[] Split(string[]? separator, StringSplitOptions options)
| Name | Description |
|---|---|
separator |
A character, a set of characters or a string (or set of strings). With null or an empty array the separators are white-space characters. |
count |
Maximum number of pieces; the last piece holds the unsplit rest. |
options |
None, RemoveEmptyEntries, TrimEntries (.NET 5+), or both combined. |
Returns: a string[] with at least one element.
Errors: ArgumentOutOfRangeException if count is negative; NullReferenceException if the string is null.
Usage notes
- Empty pieces are kept by default.
",".Split(',')returns two empty strings, and"".Split(',')returns an array with one empty string, not an empty array. UseRemoveEmptyEntriesif you want none. - Order of the options. Trimming happens first, then entries that became empty are removed when
RemoveEmptyEntriesis also set. - Whole-string separators are matched left to right without overlap. A separator that does not occur returns the original text as the only element.
- Limit with
count.Split('=', 2)is the safe way to splitkey=valuewhen the value may contain=. - No quote or escape handling.
Split(',')breaks a quoted CSV field containing a comma. Use a real parser for such data. - Allocations.
Splitcreates an array plus one string per piece. On hot paths, useMemoryExtensions.Spliton aReadOnlySpan<char>(.NET 9), which yields ranges without allocating, orIndexOf-based loops. - Regex alternative.
Regex.Splithandles patterns such as "any run of digits" or keeps separators in capture groups. - Reverse operation. String.Join() glues the pieces back.
- Lines. For line breaks prefer
EnumerateLines()orStringReader(Lines and line endings) overSplit('\n'), which leaves\rbehind on Windows text.