要使用正则表达式拆分字符串,请使用Regex.split。
假设我们的字符串是-
string str = "Hello\r\nWorld";
现在使用Regex.split分割字符串,如下所示-
tring[] res = Regex.Split(str, "\r\n");
以下是在C#中使用正则表达式拆分字符串的完整代码。
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);
}
}
}输出结果
Hello World