The System.String.Split method breaks a string into an array of substrings using one or more separators. It's the simplest way to parse delimited text such as words, CSV-style values, or protocol tokens.
The method has many overloads, but they cover four independent decisions:
- Separators: one
char, an array ofchar, onestring, or an array ofstring. - Maximum result count: cap the number of substrings returned.
- Empty-entry handling: keep empty substrings (the default) or drop them with System.StringSplitOptions.RemoveEmptyEntries.
- Whitespace handling: trim leading and trailing whitespace from each entry with System.StringSplitOptions.TrimEntries.
Split a string into words
To split a phrase on whitespace, pass ' ' as the separator:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csSplitWords
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Iterate the returned array with for to recover the position of each word:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csIndexWords
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}If the input contains repeated instances of the separator character, Split produces empty entries, one for each "gap" between consecutive separators:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csRepeatedSeparators
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Pass StringSplitOptions.RemoveEmptyEntries to drop those empty entries, as shown later in this article.
Split on multiple separator characters
When more than one character can act as a separator, pass them as an array. The following example treats spaces, commas, periods, colons, and tabs all as word boundaries:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csMultiChar
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Adjacent separators still produce empty entries:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csMultiCharGaps
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Split on multicharacter separators
To split on whole-word or multicharacter separators, pass an array of strings. The string-array overloads require a System.StringSplitOptions value. Use RemoveEmptyEntries when repeated separators would otherwise produce empty results:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csStringSeparators
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Limit how many substrings you get back
Pass a count argument to cap the number of results. The final entry holds everything that's left, including any remaining separators:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csLimitCount
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}This pattern is handy for key=value pairs and other formats where only the first separator is meaningful.
Trim whitespace from each entry
StringSplitOptions.TrimEntries strips leading and trailing whitespace from every returned substring. You can combine it with RemoveEmptyEntries for typical CSV-style cleanup:
sample · csharpdocs/csharp/fundamentals/strings/common-tasks/snippets/split/Program.csTrimEntries
namespace SplitStrings;
public static class Program
{
public static void Main()
{
SplitWords();
Console.WriteLine();
IndexWords();
Console.WriteLine();
RepeatedSeparators();
Console.WriteLine();
MultiChar();
Console.WriteLine();
MultiCharGaps();
Console.WriteLine();
StringSeparators();
Console.WriteLine();
LimitCount();
Console.WriteLine();
TrimEntries();
}
private static void SplitWords()
{
// <SplitWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox>
// => <jumps>
// => <over>
// => <the>
// => <lazy>
// => <dog.>
// </SplitWords>
}
private static void IndexWords()
{
// <IndexWords>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine($"Index {i}: <{words[i]}>");
}
// => Index 0: <The>
// => Index 1: <quick>
// => Index 2: <brown>
// => ...
// </IndexWords>
}
private static void RepeatedSeparators()
{
// <RepeatedSeparators>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// The runs of spaces produce empty entries:
// => <The>
// => <quick>
// => <brown>
// => <>
// => <>
// => <>
// => <fox>
// => ...
// </RepeatedSeparators>
}
private static void MultiChar()
{
// <MultiChar>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo three:four,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <three>
// => <four>
// => <five>
// => <six>
// => <seven>
// </MultiChar>
}
private static void MultiCharGaps()
{
// <MultiCharGaps>
char[] delimiters = [' ', ',', '.', ':', '\t'];
string text = "one\ttwo :,five six seven";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(delimiters);
Console.WriteLine($"{words.Length} words in text:");
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => 7 words in text:
// => <one>
// => <two>
// => <>
// => <>
// => <five>
// => <six>
// => <seven>
// </MultiCharGaps>
}
private static void StringSeparators()
{
// <StringSeparators>
string[] separators = ["<<", "..."];
string text = "one<<two......three<four";
Console.WriteLine($"Original text: '{text}'");
string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine($"{words.Length} substrings in text:");
foreach (var word in words)
{
Console.WriteLine(word);
}
// => 3 substrings in text:
// => one
// => two
// => three<four
// </StringSeparators>
}
private static void LimitCount()
{
// <LimitCount>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, StringSplitOptions.None);
foreach (var word in words)
{
Console.WriteLine($"<{word}>");
}
// => <The>
// => <quick>
// => <brown>
// => <fox jumps over the lazy dog.>
// </LimitCount>
}
private static void TrimEntries()
{
// <TrimEntries>
string numerals = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
string[] trimmed = numerals.Split(',', StringSplitOptions.TrimEntries);
Console.WriteLine("Trimmed entries:");
foreach (var word in trimmed)
{
Console.WriteLine($"<{word}>");
}
string[] untrimmed = numerals.Split(',', StringSplitOptions.None);
Console.WriteLine("Untrimmed entries:");
foreach (var word in untrimmed)
{
Console.WriteLine($"<{word}>");
}
// => Trimmed entries: <1> <2> ... <10>
// => Untrimmed entries: <1> < 2> ... < 10>
// </TrimEntries>
}
}Use regular expressions
Split works well for fixed character or string delimiters. For pattern-based splitting, use System.Text.RegularExpressions.Regex.Split. For an introduction to regular expressions on strings, see String operations.