find the word

im trying to make the FIND& REPLACE functions of the any Notepad

i used fo FIND :

// the code

String findThisWord = JOptionPane.showInputDialog(myTextArea,"Find:",

"Find",JOptionPane.PLAIN_MESSAGE);

int ff= findThisWord.compareTo(myTextArea.getText());

System.out.println(ff);

// the end

the output is not understood, can any one help me to get it or suggest another way

& the same for the REPLACE function (im thinking about using replaceRange())

[518 byte] By [TheInsider] at [2007-9-30 20:35:03]
# 1

Hi

I dont think String.comareTo is the right function to be used in this case.

try

String.indexOf()

usage:

myTextArea.getText().indexOf(findThisWord);

if "findThisWord" is not present, it'll return -1.

there is an alternative too...

try using String.matches();

this function takes a regular xpression as an argument. the advantage of this will be that u would be able to use String.matches().replaceAll(); or String.matches().replace();

usage:

myTextArea.getText().matches(".*" + findThisWord + ".*");

All the best

kapilChhabra

kapilChhabra at 2007-7-7 1:24:32 > top of Java-index,Administration Tools,Sun Connection...
# 2

The following code will find a text the specified word from the cursor(caret) position in the textarea.

String wordToFind;

int caretPos=textArea.getCaretPosition();

int i;

for(i=caretPos; i<(textArea.getText().length()-wordToFind.length()); i++)

{

String temp=textArea.getText().substring(i,i+wordToFind.length());

if (temp.equals(wordToFind))

{

textArea.select(i,i+wordToFind.length());

break;

}

}

The above code can be put in a method and should be called each time u want to a word.

Aswin at 2007-7-7 1:24:32 > top of Java-index,Administration Tools,Sun Connection...