str = "R57 & R1 & R11"
string.Replace("R1","true")
I want to replace only R1 => true
generated Output = "R57 & true & true1"
Correct Output = "R57 & true & R11"
What about this,
var str = "R57 & R1 & R11";
var result = str.Replace(" R1 "," true ") //Look at spaces before and after "R1"
or
using System.Text.RegularExpressions;
...
string result = Regex.Replace(str, @"\bR1\b", "true"); //`\b` denotes boundary
RegEx, str.Replace() is just a hack. I am glad you choose RegEx over str.Replace() Use of \b is explained by @DmitryBychenko. \b denotes match must be present in between boundary
string result = Regex.Replace(str, @"\bR1\b", "true");