String text4 = "Hello * welcome to * C#" įoreach (var tokens in text4.Split("(*)")) String text3 = "Hello * welcome to * C#" Ĭonsole.WriteLine("With delimiters =") With String.Split() function, you simply need to pass the delimiter that you want to use to split a string. String.Split()Īlternatively, you can use String.Split() function to split a string. The output clearly shows that the first script doesn’t include delimiters while the second script included delimiter while splitting a string. Notice round brackets in the regular expression, these round brackets tell the Split() function to include the delimiter in the split string. Next, in order to include delimiter asterisk in the out, the regex expression splits the string. In the output, the asterisk itself is not included. The Regex expression used is This Regex expression splits a string at indexes where an asterisk occurs.
The first parameter to the Regex.Split() function is the text string, while the second parameter is the Regex expression that is to be matched.
Let’s see a simple example of how to split a string with the Regex.Split() function.įoreach (var tokens in Regex.Split(text, delimiters =\n") įoreach (var tokens in Regex.Split(text, the script above, the Regex.Split() function splits the text “Hello*welcome to*C#” using the asterisk as a delimiter.
Furthermore, In C# String.split() function also splits a string. The Split() function from Regex can be used to split a string in C#. In this section, you will see both ways of splitting strings in C#. The split() functions from both the String class and the Regex library returns a string split into tokens.
Webinar – Free training webinar at PDF.co API platform.