In our day to day scenarios, we sometimes end up in a scenario where we have to split a delimited string to a list.
If you are using the C# language, splitting a string is an easy task.
In this blog post, we will learn how we can split a string in C# using the string.Split() method and we will also use a delimiter such as a comma or a space to split the string into a list or an array.
String.Split() Method Explained
In C#, we can use the .Split() method on the sealed class String.
Syntax:
The Split method provides around ten overloads. A few are listed below.
string.Split(char[]? separator, int count) string.Split(char[]? separator, int count, StringSplitOptions options) string.Split(char[]? separator, StringSplitOptions options) string.Split(string? separator, StringSplitOptions options = StringSplitOptions.None) string.Split(string? separator, int count, StringSplitOptions options = StringSplitOptions.None) string.Split(string[]? separator, StringSplitOptions options)
Return:
The string split method always returns an array of strings based on the separator.
string[]
Split String In C# - Code Examples
Split A String Using A String Separator In C#
In this we will use the string.Split(string? separator, StringSplitOptions options = StringSplitOptions.None) method.
In this method, we are only required to pass the string separator by which we want to slpit the string.
Let's see this in an example:
// String separated by comma string dayOfWeekString = "Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"; // Split string by string string[] dayOfWeekArray = dayOfWeekString.Split(","); // Display on Console foreach (var dayOfWeek in dayOfWeekArray) { Console.WriteLine(dayOfWeek); }
Output:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Split A String Using A String Separator And TrimEntries StringSplitOptions In C#
In this we will use the string.Split(string? separator, StringSplitOptions options = StringSplitOptions.None) method.
In this method, along with the string delimiter, we will also pass the string split options We will use the trim entries options which trims the white-space from each entry.
Let's see this in an example:
// String separated by comma with white-space around some of the entries string dayOfWeekString = "Monday , Tuesday , Wednesday , Thursday , Friday,Saturday,Sunday"; // Split string and trim entries by string string[] dayOfWeekArray = dayOfWeekString.Split(",", StringSplitOptions.TrimEntries); // Display on Console foreach (var dayOfWeek in dayOfWeekArray) { Console.WriteLine(dayOfWeek); }
Output:
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Split A String Using A String Separator And RemoveEmptyEntries StringSplitOptions In C#
In this we will use the string.Split(string? separator, StringSplitOptions options = StringSplitOptions.None) method.
In this method, along with the string delimiter, we will also pass the string split options We will use the trim entries options which trims the white-space from each entry.
Let's see this in an example:
// String separated by comma and empty entry with trim options string dayOfWeekString = "Monday, ,Wednesday,Thursday,Friday,Saturday,Sunday"; // Split string and trim entries by string string[] dayOfWeekArray = dayOfWeekString.Split(",", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); // Display on Console foreach (var dayOfWeek in dayOfWeekArray) { Console.WriteLine(dayOfWeek); }
Output:
Monday Wednesday Thursday Friday Saturday Sunday