溫馨提示×

C# split()是否支持正則表達式

c#
小樊
98
2024-08-23 20:27:28
欄目: 編程語言

在C#中,split() 方法不直接支持正則表達式。然而,可以使用 Regex 類來實現基于正則表達式的字符串分割。通過使用 Regex.Split() 方法,可以將輸入字符串按照指定的正則表達式模式進行分割。以下是一個示例:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string input = "Hello,world;how are you?";
        string pattern = @"[ ,;]";

        string[] words = Regex.Split(input, pattern);

        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

在上面的示例中,我們使用正則表達式 [ ,;] 來指定分割的模式,即空格、逗號和分號。Regex.Split() 方法將輸入字符串 input 按照這個模式進行分割,并將結果存儲在一個字符串數組中。最后,我們遍歷這個數組并輸出每個分割后得到的子串。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女