How to split a string using regular expressions in C#?

CsharpProgrammingServer Side Programming

To split a string suing regular expression, use the Regex.split.

Let’s say our string is −

string str = "Hello\r\nWorld";

Now use Regex.split to split the string as shown below −

tring[] res = Regex.Split(str, "\r\n");

The following is the complete code to split a string using Regular Expression in C#.

Example

 Live Demo

using System;
using System.Text.RegularExpressions;
class Demo {
   static void Main() {
      string str = "Hello\r\nWorld";
      string[] res = Regex.Split(str, "\r\n");
      foreach (string word in res) {
         Console.WriteLine(word);
      }
   }
}

Output

Hello
World
raja
Published on 18-Sep-2018 14:07:57
Advertisements