. Advertisement .
..3..
. Advertisement .
..4..
The String.Split()
method can help you split string to list in C#. Scroll down to find out how.
Split String To List In C# With String.Split()
The String.Split()
method of the System namespace can split elements of a string or character array using one or multiple delimiters and return those substrings in a string array. This is the easiest solution for separating C# strings on word boundaries.
The simplest overload of this method:
public string[] Split (params char[]? separator);
In this use case, we only pass one parameter: separator. It needs to be an array of characters or null. The Split()
method will use them as delimiting characters to separate the original strings into substrings. The return value is an array of strings whose elements store those substrings.
This is an example of splitting a phrase into words and putting them into an array of strings:
using System;
public class ITTutoria{
static void Main(string[] args)
{
string phrase = "Welcome to ITTutoria.net!";
string[] words = phrase.Split(' ');
foreach (var word in words)
{
System.Console.WriteLine(word);
}
}
}
Output:
Welcome
to
ITTutoria.net!
In the above example, the Split()
method uses a space character as the delimiter to separate our phrase into words. Each of those words will become an element of the returned array, which the program prints to the output using the foreach loop.
Remember that even if you don’t provide a delimiter, the method still returns the same result in this case. Without delimiting characters specified, String.Split() will split the string at whitespace characters.
The example above should work with simple phrases. But as you can see, it also includes an exclamation mark (!). This is also the case with other period characters.
If you want to remove them from the substring, you can add them to the Split()
method as another delimiting character like this:
using System;
public class ITTutoria{
static void Main(string[] args)
{
string phrase = "Welcome to ITTutoria.net!";
char[] separators = new char[] { ' ', '!' };
string[] words = phrase.Split(separators, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
{
System.Console.WriteLine(word);
}
}
}
Output:
Welcome
to
ITTutoria.net
The StringSplitOptions.RemoveEmptyEntries
option is included to remove empty strings from the returned array. If you omit it, there will be an empty substring in the resulting array, representing the substring between ‘net’ and ‘!’.
This occurs because the Split()
method always produces a value in the resulting array by default when it encounters an instance of the delimiter characters, even when it means the value is an empty substring. This is another common case where you may have empty strings in the returned value:
using System;
public class ITTutoria{
static void Main(string[] args)
{
string phrase = "Welcome to ITTutoria.net!";
char[] separators = new char[] { ' ', '!' };
string[] words = phrase.Split(separators);
foreach (var word in words)
{
System.Console.WriteLine(word);
}
}
}
Output:
Welcome
to
ITTutoria.net
Again, you can change this behavior with the StringSplitOptions.RemoveEmptyEntries
parameter to omit any empty string in the resulting array.
Keep in mind that the Split()
method isn’t always the best solution for breaking a string into several substrings. If you want to parse a string using a pattern instead of a delimiter, or when you want to extract just some of the substrings, consider other options like regular expressions.
Conclusion
The String.Split()
method is designed to help .NET developers split string to list in C#. You can use one or multiple delimiting characters to separate substrings and put them in a string array. From this array, you can easily convert them to, for instance, a JSON object. If this is also your goal, check out this guide.
Leave a comment