如何在 C# 中使用正則表示式拆分字串?
要使用正則表示式拆分字串,請使用 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
廣告