This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Find and Replace  RSS feed

 
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.
 
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.
 
(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?
 
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
 
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?

 
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
Boost this thread!