StringBuffer replace question
Hi,
I'm trying to take a stringBuffer, make a match on part of it, and replace all instances of the match with another string.
I was able to do it replacing one name with another, but when I try the same technique for replacing "o" with "oo" I get an infinite loop, and I can't figure out why. Have searched tutorials and forum and couldn't find the answer.
here's the code:
publicclass BufTest{
publicstaticvoid main(String[] args){
StringBuffer test =new StringBuffer("Hi Larry, what's up Larry, come on over, Larry.");
String match ="Larry";
String replace ="Bill";
System.out.println("unaltered value of 'test': " + test);
System.out.println("value of 'replace': " + replace);
// see if I can isolate out 'larry'
String temp = test.substring(test.indexOf(match), (test.indexOf(match) + match.length()));
System.out.println("value of 'temp': " + temp);
// if the match is made, test.indexOf(match) will have a value
System.out.println("value of test.indexOf(match): " + test.indexOf(match));
//if the match is not made, the value will be -1
System.out.println("value of test.indexOf(replace): " + test.indexOf(replace));
//replace larry with bill
while (test.indexOf(match) != -1){
test.replace(test.indexOf(match), (test.indexOf(match)+ match.length()), replace);
}
System.out.println("value of 'test' after replace: " + test);
// the o and oo thing
StringBuffer test2 =new StringBuffer("Would you help me with my homework John, would you John? Why not John?");
String match2 ="o";
String replace2 ="oo";
System.out.println("unaltered value of test2: " + test2);
//find an 'o'
String temp2 = test2.substring(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()));
System.out.println("value of temp2: " + temp2);
System.out.println("value of index for o: " + test2.indexOf(match2));
test2.replace(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()), replace2);
System.out.println("value of test2 after replace: " + test2);
System.out.println("value of index: " + test2.indexOf(match2));
test2.replace(test2.indexOf(match2), (test2.indexOf(match2) + match2.length()), replace2);
System.out.println("value of test2 after 2nd replace: " + test2);
System.out.println("value of index: " + test2.indexOf(match2));
// inside a while loop, the statement below causes an infinite loop
//System.out.println("inside while loop: " + test2.indexOf(match2));
}
}
it produces this on the console:
unaltered value of 'test': Hi Larry, what's up Larry, come on over, Larry.
value of 'replace': Bill
value of 'temp': Larry
value of test.indexOf(match): 3
value of test.indexOf(replace): -1
value of 'test' after replace: Hi Bill, what's up Bill, come on over, Bill.
unaltered value of test2: Would you help me with my homework John, would you John? Why not John?
value of temp2: o
value of index for o: 1
value of test2 after replace: Woould you help me with my homework John, would you John? Why not John?
value of index: 1
value of test2 after 2nd replace: Wooould you help me with my homework John, would you John? Why not John?
value of index: 1
thanks,
bp

