如何使用 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

更新於:22-Jun-2020

987 瀏覽次數

Kickstart 你的職業生涯

完成課程以獲得認證

開始
廣告