สมมติว่าสตริงของเราคือ −
string str = "abcd$ef$gh";
หากต้องการแทนที่อักขระพิเศษ ให้ใช้เมธอด Replace()
string res = str.Replace('$', 'k'); ต่อไปนี้เป็นรหัสที่สมบูรณ์เพื่อแทนที่อักขระจากสตริง -
ตัวอย่าง
using System;
public class Program {
public static void Main() {
string str = "abcd$ef$gh";
Console.WriteLine("Initial string = " + str);
string res = str.Replace('$', 'k');
// after replacing
Console.WriteLine("Replaced string = " + res.ToString());
}
} ผลลัพธ์
Initial string = abcd$ef$gh Replaced string = abcdkefkgh