Splitting
We can split any string into separate parts based on a specified character.
The Split
method returns an array as the result:
var list = "coconut,melon,avocado";
var fruits = list.Split(',');
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
If there are empty items in the resulting array, we can remove them.
Notice the new array for the separators / delimiters.
var list = "coconut,,melon,avocado,";
var charSeparators = new char[] { ',' };
var fruits = list.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
In N Parts
We can set the maximum number of substrings which will be returned:
var list = "coconut,,melon,avocado,";
var charSeparators = new char[] { ',' };
var fruits = list.Split(charSeparators, 2);
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
/* The output of this will be:
coconut
,melon,avocado,
*/
TIP
If you want, you can add StringSplitOptions.RemoveEmptyEntries
because there is an overload for it.
By Strings
Instead of a simple character char
, we can have strings as delimiters, but we need to specify the splitting options (in this case None
):
var list = "strawberry,blueberry,raspberry";
var stringSeparators = new string[] { "berry" };
var fruits = list.Split(stringSeparators, StringSplitOptions.None);
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
Note
For all of these splitting examples, if the delimiter is null, then the compiler will take the whitespace for the delimiter by default.