實例場景,針對字串:"AAAA AAA BBBB BBB BBB CCCCCCCC"。
1. 分隔為“AAAA AAA” , "BBBB BBB BBB","CCCCCCCC"
2. 分隔為“AAAA" ,”AAA“,”BBBB“,"BBB", "BBB",」CCCCCCCC「
實作程式碼:
void Main() { var str = "AAAA AAA BBBB BBB BBB CCCCCCCC"; // - split by multiple spaces(more than one) var val = System.Text.RegularExpressions.Regex.Split( str, @"\s{2,}"); System.Console.WriteLine(val); // - split by spaces(one or more) var val2 = System.Text.RegularExpressions.Regex.Split( str, @"\s{1,}"); System.Console.WriteLine(val2); }
以上就是C# split字串根據1個或多個空格的內容,更多相關內容請關注PHP中文網(www .php.cn)!