String.replaceAll(str, str) is leading to infinite loop. Help?
The String variable body is assigned a value by the user and then this code should be replacing any number of carriage returns with a single string "<*>".
If I run just the first replaceAll() then it works fine. If I run either of the codes blow then the program stalls.
Code:
body = body.replaceAll("\n", "<*>");
Code:
body = body.replaceAll("<*><*>", "<*>");
I also tried a while loop
Code:
while(body.indexOf("<*><*>") != -1)
body.replaceNext("<*><*>", "<*>");
Like I said, they all ended in a stalled program.
Any help would be greatly appreciated!
Here is a compilable test code:
Code:
public class Test {
public static void main(String[] args){
String body = "This is a \n\n test of a code I would like\n\n to work.. \nbut\n\n it is not working.\n";
System.err.println(body);
body = body.replaceAll("\n", "<*>");
System.err.println(body);
body = body.replaceAll("<*><*>", "<*>");
System.err.println(body);
}
}
For some reason it is not stalling in this but it is also not replacing anything. Thoughts?