如何在 C# 中使用正则表达式拆分字符串?

csharpprogrammingserver side programming

要使用正则表达式拆分字符串,请使用 Regex.split。

假设我们的字符串是 −

string str = "Hello\r
World";

现在使用 Regex.split 拆分字符串,如下所示 −

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

以下是在 C# 中使用正则表达式拆分字符串的完整代码。

示例

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

输出

Hello
World

相关文章