• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find and Replace

 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic