It's not to do with the spaces, it's because of the upper-cased D.
text.toLowerCase(Locale.ROOT)
will make a string containing only lower-cased letters, so "Do not repeat"
will not be found in it.
You can make replaceAll
case insensitive by passing the appropriate flag:
text = text.replaceAll("(?i)" + check, "WRONG");
Note that you might run into problems with metacharacters in the strings you are searching for. If you might include things with e.g. periods (.
), you should quote check
:
text = text.replaceAll("(?i)" + Pattern.quote(check), "WRONG");
Also, because you're not considering word boundaries, you might run into the Scunthorpe problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…