Question

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
Was it helpful?

Solution

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 ""

OTHER TIPS

You should make sure that Constants.GENE_START_SEQUENCE is a valid regex pattern. If it isn't supposed to be a regular expression you should escape it using the quote method on java.util.regex.Pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top