Dùng Regex.Replace để xóa ký tự cuối chuỗi (ví dụ: dấu chấm than). Thay thế bằng chuỗi rỗng.
Ví dụ
using System;
using System.Text.RegularExpressions;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
string s1 = "Demo Text!";
// Xóa dấu ! ở cuối
string s2 = Regex.Replace(s1, "!", "");
Console.WriteLine("\"{0}\"\n\"{1}\"", s1, s2);
}
}
}
Kết quả
"Demo Text!" "Demo Text"
Mẹo: Để xóa ký tự cuối cùng bất kỳ, dùng s1.Substring(0, s1.Length - 1) hiệu quả hơn regex.