SEARCH&REPLACE in a textBox

  • Thread starter Thread starter Tomomichi Amano
  • Start date Start date
T

Tomomichi Amano

Hello.
I want to make replace & search functions in my text editor. Thanks to the
kind people here at the newsgroup, I was able to make the [REPLACE ALL]
function.
But I was not able to understand how to REPLACE the next word (the nearest
word from the cursor; not REPLACE ALL, but replace only one word) and SEARCH
the next word.
COuld some one help me? Thanks in advance.
 
Use the position of the cursor in the text string and find the first occurrence of the search word after that index:

String theText, searchWord, replaceWord;
int cursorPos;

// TODO:
// set the values for each variable above

int foundPos = theText.IndexOf(searchWord, cursorPos);

if (foundPos != -1) {
// if the word was found, copy the preceding text,
// sub the word and copy the trailing text

theText = theText.Substring(0, foundPos) + replaceWord +
theText.Substring(foundPos + searchWord.Length);
}


I hope this helps,
-JG
 
Back
Top