正则表达式是最简单的方法:
string input = "test, and test but not testing. But yes to test";string pattern = @"btestb";string replace = "text";string result = Regex.Replace(input, pattern, replace);Console.WriteLine(result);
模式的重要部分是
b元字符,它在单词边界上匹配。如果您需要不区分大小写,请使用
RegexOptions.IgnoreCase:
Regex.Replace(input, pattern, replace, RegexOptions.IgnoreCase);



