Click to See Complete Forum and Search --> : Replace a word within a string without using replace function


jingsee
10-07-2005, 04:00 PM
Hi,

How can i take a string lets say "sentence" and a word wordOne and an other word wordTwo. And replace within sentence wordOne by wordTwo without using the function


sentence.replace(wordOne, wordTwo);

Is it possible to maybe chop off the string into diffewrent part match the word and swap it then reconstruct the string and display.

Thanks
JD

chimps
10-08-2005, 10:29 AM
Hi,
Is it possible to maybe chop off the string into diffewrent part match the word and swap it then reconstruct the string and display.

Thanks
JD

Check out the subString StringTokenizer class to divide the sentence into a set of words, then you can just iterate through them to see which ones you're looking for and create a new String object with the final (switched) result:

StringTokenizer: http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html