ตั้งค่าสตริง −
string str = "Bit and Bat";
สมมติว่าคุณต้องแทนที่สิ่งที่อยู่ภายใน B และ t เป็น A และทำให้สตริงทั้งหมดเป็นตัวพิมพ์ใหญ่ เพื่อที่จะใช้แทนที่ −
Regex.Replace(str, "B.t", "BAT");
ให้เราดูรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Text.RegularExpressions;
namespace Demo {
class Program {
static void Main(string[] args) {
string str = "Bit and Bat";
Console.WriteLine(str);
string res = Regex.Replace(str, "B.t", "BAT");
Console.WriteLine(res);
}
}
} ผลลัพธ์
Bit and Bat BAT and BAT