I'm attempting to replace all instances of a particular String with a unique replacement.
What I would like:
If I have this String:
String testScript = "while(true) { } while (10 < 7) { } while((10 < 7)) { }";
I would like this output:
while(arg0 < 5000 && true) { } while(arg1 < 5000 && 10 < 7) { } while(arg2 < 5000 && (10 < 7)) { }
What I have:
However, the string passed in to replaceAll
doesn't get queried again (obvious now I think about it).
while(arg0 < 5000 && true) { } while(arg0 < 5000 && 10 < 7) { } while(arg0 < 5000 && (10 < 7)){ }
Any answers or comments, as always, are greatly appreciated.
SSCCE:
public static void main(String[] args) {
int counter = 0;
String testScript = "while(true) { } while (10 < 7) { } while((10 < 7)) { }";
String out = testScript.replaceAll("while\s*\(", "while(arg" + (counter++) + " < 5000 && ");
System.out.println(out);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…