I'm trying to cleanse one String from another.
before = before.replaceAll(Constants.GENE_START_SEQUENCE, "");
And yet, the following assertion sometimes fails:
assert before.indexOf(Constants.GENE_START_SEQUENCE) == -1 : before;
This is what the assert spits out:
IIAOOOCOAAAOCCIOOOACAIAOACICOOIAIOOICIIOIIOICOICCCOOAOICOCOOIIOOAOAACIIOCCICIOIII
replaceAll
only replaces occurences of the pattern in the original string. If the pattern reoccurs as a result of the replacement, this new occurence won't be replaced. Example:
"XXYY".replaceAll("XY", "");
This will find one occurence of "XY" (at index 1) and then replace that with "". The result will be "XY". If you want to prevent this from happening, you have to rexecute replaceAll, until replaceAll
stops finding a match.
String string = "XXYY";
String oldString;
do {
oldString = string;
string = string.replaceAll("XY", "");
} while(!string.equals(oldString));
// string will now be ""