Use Regex.Replace() method for conditional replace.
To Replace the whole word only use the regular expression "\bYourWord\b".
In a regular expression \b means "word boundary".
Example:-
using System.Text.RegularExpressions
string x = "I am Soham";
x = x.Replace("am","xx");
Output : I xx Sohxx
string x = "I am Soham";
x = x.Regex.Replace("am","\bam\b","xx",RegexOptions.IgnoreCase);
Output : I xx Soham
To Replace the whole word only use the regular expression "\bYourWord\b".
In a regular expression \b means "word boundary".
Example:-
using System.Text.RegularExpressions
string x = "I am Soham";
x = x.Replace("am","xx");
Output : I xx Sohxx
string x = "I am Soham";
x = x.Regex.Replace("am","\bam\b","xx",RegexOptions.IgnoreCase);
Output : I xx Soham
No comments:
Post a Comment