ตั้งค่าสตริง −
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