Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replacer

 
Roi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<reply>
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...
</reply>
Cheers :-)-
s a h i l
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://www.javaranch.com/ubb/Forum33/HTML/000518.html
You run the SNR.java 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.
Goodbye
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic