Java String.replace/replaceAll not working

Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:

str = str.replaceAll("\\[", "");

For the normal replace method, you don’t need to escape the bracket:

str = str.replace("[", "");

Leave a Comment