Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Cách lấy 2 ký tự cuối chuỗi trong C# dùng Regex


Khởi tạo chuỗi:

string str = "Cookie and Session";

Sử dụng Regex sau để lấy 2 ký tự cuối:

Regex.Match(str, @"(.{2})\s*$")

Mã nguồn hoàn chỉnh:

Ví dụ

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*$"));
    }
}

Đầu ra

on