C# How To Split a String
Split a String
Use the Split() method to split a string into words:
Example
string text = "apple banana cherry";
string[] words = text.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
To split at another character, like a comma, use that character as the separator:
Example
string text = "red,green,blue";
string[] colors = text.Split(',');
foreach (string color in colors)
{
Console.WriteLine(color);
}
Explanation:Split() cuts the string at each separator, and returns the parts in an array.
The separator itself (the space or the comma) is not part of the result.