我試圖取代用下面的代碼字符串的一部分字符串的確切部分:C#替換包含特殊字符
public static string SafeReplace(this string input, string find, string replace, bool matchWholeWord)
{
string textToFind = matchWholeWord ? string.Format(@"\b{0}\b", find) : find;
return Regex.Replace(input, textToFind, replace);
}
但是,當我有字符串中的特殊字符,這並不工作。我試圖逃避的人物,但沒有運氣...
下面是一個例子,我有以下字符串:
Peter[='111222'] + APeter[='111222']
我想@
更換Peter[='111222']
所以結果應該是: @ + APeter[='111222']
。使用給定的代碼,字符串保持不變,沒有任何變化。
請注意,我可能會有許多不同的情況與其他特殊字符,如Steven.intr[A:B;>1], Sssdf.len, asd.ind
等,所以在我的情況下,我需要找到不同格式的精確匹配。
在此先感謝!
你可以張貼一些例子 – Rohit
肯定:喬治[= 1],QC.ind,asd.len。它應該與不包含特殊字符的字符串一起使用。以下是我需要替換的另一個字符串示例:「asd.len + hasd.len」 - >替換「asd.len」 - >結果:「@ + hasd.len」。 – Venco