C# Replace字符替换函数

 它可以将字串内的字符替换为别的字符,可以嵌套使用,如下:

 需要注意的是,它可以把字符替换为空,但不可以替换空字符,当不确定字符串是否为空时,可以进行以下判断,再替换:

 示例的完整代码:

string aa="d";
if (aa=="d")
{
Console.WriteLine(aa.Replace("d", "a"));
}

//string b = aa.Replace(".", "").Replace("officeword", "doc").Replace("gw", "other") == "" ? "other" : aa.Replace(".", "").Replace("officeword", "doc").Replace("gw", "other");
// Console.WriteLine(b);

猜你喜欢

转载自www.cnblogs.com/myka/p/11988524.html