The moose likes I/O and Streams and the fly likes String replacer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "String replacer" Watch "String replacer" New topic

String replacer


Joined: Sep 20, 2000
Posts: 2
I need to replace a string by another string in text paragraph. Is anymethod already available to do this? I am using jdk1.2.2/jdk1.3. Thanks
sahil chandran

Joined: Sep 15, 2000
Posts: 3
u can us setText(String s);
to set the text of the TextaArea or TextField , whtever is the
class of Text paragraph u talked offf...
Cheers :-)-
s a h i l
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Hi Roi,
Unfortunately, there is no method that does this directly, in the Java APIs.
If you need to replace a "word" by another word in a any text string, one way is to use a StringTokenizer to take out all words (token) from your string, one at a time. Then build back that string from scratch, replacing the searched word by its replacement word.
You can have sample code at:
You run the with a text file name, the searched word, and the replacement word, and it does the job without confusion.
For example, say the text:
I read one book this week. Book? Rather a good book! I found it at the bookstore. How many books(did you write?)...
Then replace the word book with newspaper, you'll get the resulting text:
I read one newspaper this week. Book? Rather a good newspaper! I found it at the bookstore. How many books(did you write?)...
Notice that the resulting text kept "Book", "bookstore", and "books". This is strict word replacement. But you could change the program slightly.
I agree. Here's the link:
subject: String replacer
It's not a secret anymore!