Sử dụng phương thức Replace () để thay thế một ký tự bằng dấu hoa thị.
Giả sử chuỗi của chúng tôi là -
string str = "dem* text";
Để thay thế nó, hãy sử dụng phương thức Replace () -
str.Replace('*', 'o'); Đây là mã hoàn chỉnh -
Ví dụ
using System;
public class Program {
public static void Main() {
string str = "dem* text";
Console.WriteLine("Initial string = " + str);
string res = str.Replace('*', 'o');
// after replacing
Console.WriteLine("After replacing asterisk = " + res.ToString());
}
} Đầu ra
Initial string = dem* text After replacing asterisk = demo text