aspose file tools*
The moose likes Java in General and the fly likes Find and Replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find and Replace" Watch "Find and Replace" New topic
Author

Find and Replace

Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
I am trying to make a method that replaces words. It uses a tokenizer to check all of the strings against the first short string, if it finds a match then it changes the word to the second string entered. I have everything working except I dont know how to modify a string to place the new word where the old one was. I couldnt work out which would be easier to use, a tokenizer or a string. Help with either would be great ty.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
if I understand your post right, you can use a StringBuffer
inside the loop,
for each token, append the original sub-string if it does not
match the first short string or append the 2nd string if a match found.
then call toString() to convert the StringBuffer to String.


not so smart guy still curious to learn new stuff every now and then
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The common way to change a word in a string is

Once you have all those substringing things down, you can put the last three lines into one, or leave them separate for clarity.

BTW: It's common practice in this forum to give vague answers that invite you do to some homework on the APIs. Lemme know if that was TOO vague or just enough to have some fun with the JavaDoc. Also feel free to post the code you have running (or almost running) for more specific comments.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
K guys thanks, im using the orignal tokenizer idea, however now it says that the StringTokenizer class isnt correct, ive checked the spelling and my sytanx alright which package is it in?
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
Depending on what you're trying to do, and/or what jdk you're writing for, it sounds like you might find regular expressions handy. There's a tutorial over at sun:
http://java.sun.com/docs/books/tutorial/extra/regex/
Peter Shipway
Ranch Hand

Joined: Mar 25, 2004
Posts: 71
Ok one last problem how do i use the equals method in the string class? Could someone give me an example plz? ty
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



http://home.arcor.de/hirnstrom/bewerbung
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
java.util
Originally posted by Peter Shipway:
K guys thanks, im using the orignal tokenizer idea, however now it says that the StringTokenizer class isnt correct, ive checked the spelling and my sytanx alright which package is it in?
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
import java.util.*;


The nice thing about Standards is that there are so many to choose from!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find and Replace