How to split a string using the string delimiter in C#
We can split a string with a string delimiter by using the String.Split()
method in C#.
Here is an example that splits the string with a string awesome
:
using System;
class SplitStringProgram {
static void Main() {
string msg = "This is big awesome quote from einstein";
string[] arr = msg.Split(new[] { "awesome" }, StringSplitOptions.None);
foreach (string val in arr) {
Console.WriteLine(val);
}
}
}
Output:
This is big
quote from einstein
If a string delimiter contains a single character like ,
or ;
we can split it by just passing character to the Split()
method.
using System;
class SplitStringProgram {
static void Main() {
string msg = "Welcome, to, the programming world";
string[] arr = msg.Split(',');
foreach (string val in arr) {
Console.WriteLine(val);
}
}
}
Output:
Welcome
to
the programming world