Reputation: 1021
my original replace code is.
rtTextArea.Rtf = rtTextArea.Rtf.Replace(oldtext, newtext);
but the problem is it replaces all occurence of the words instead of the required word e.g origianl word :- hello my name is serak and am in israel replace() -- is with are the whole word chages to "are serak and am in arereal"
is there anyway i can include a condition or something to match the whole word? my working environment C#.
Upvotes: 0
Views: 1434
Reputation: 2274
Using RegEx for this is the correct approach, but just for brevity you could also prepend/append a space to the word you want to replace to ensure it is not a part of another word.
e.g. wordToReplace = " " + wordToReplace + " ";
Upvotes: 0
Reputation: 140
The previous post showed
var regExp = new System.Text.RegularExpressions.Regex(@"\b"+oldtext+@"\b"); rtTextArea.Rtf = regExp.Replace(rtTextArea.Rtf, newtext);
Which is a little different from the first post. Notice the extra b and @
This worked for me.
Upvotes: 0
Reputation: 4495
Use Regular expressions. In this example the \b
represents a Word Boundary.
var regExp = new System.Text.RegularExpressions.Regex(@"\bis\b");
rtTextArea.Rtf = regExp.Replace(rtTextArea.Rtf, "are");
Upvotes: 1