A regex is the easiest approach:
string input = "test, and test but not testing. But yes to test";
string pattern = @"test";
string replace = "text";
string result = Regex.Replace(input, pattern, replace);
Console.WriteLine(result);
The important part of the pattern is the
metacharacter, which matches on word boundaries. If you need it to be case-insensitive use RegexOptions.IgnoreCase
:
Regex.Replace(input, pattern, replace, RegexOptions.IgnoreCase);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…