I have a problem while trying to replace all text matching a particular word in a rich text box
. This is the code i use
public static void R
Simply do this:
yourRichTextBox.Text = yourRichTextBox.Text.Replace("e","ea");
If you want to report the number of matches (which are replaced), you can try using Regex
like this:
MessageBox.Show(Regex.Matches(yourRichTextBox.Text, "e").Count.ToString());
Of course, using the method above has expensive cost in memory, you can use some loop in combination with Regex
to achieve some kind of advanced replacing engine like this:
public void ReplaceAll(RichTextBox myRtb, string word, string replacement){
int i = 0;
int n = 0;
int a = replacement.Length - word.Length;
foreach(Match m in Regex.Matches(myRtb.Text, word)){
myRtb.Select(m.Index + i, word.Length);
i += a;
myRtb.SelectedText = replacement;
n++;
}
MessageBox.Show("Replaced " + n + " matches!");
}