如何使用 Regex 從 C# 字串中獲取最後 2 個字元?
設定字串 -
string str = "Cookie and Session";
使用以下 Regex 從字串中獲取最後 2 個字元 -
Regex.Match(str,@"(.{2})\s*$")
以下為程式碼 -
示例
using System; using System.Text.RegularExpressions; public class Demo { public static void Main() { string str = "Cookie and Session"; Console.WriteLine(Regex.Match(str,@"(.{2})\s*$")); } }
輸出
on
廣告