I have a String str, I want to strip off all the following special characters {}- using Java.regex and replaceAll().
I would do like that:
str.replaceAll("[\\{\\}\\-]","");
but it doesn't strip what I ask for. Why?
I have a String str, I want to strip off all the following special characters {}- using Java.regex and replaceAll().
I would do like that:
str.replaceAll("[\\{\\}\\-]","");
but it doesn't strip what I ask for. Why?
Strings are immutable in Java, meaning str won't be modified by calling replaceAll. You need to re-assign the new value to the string:
str = str.replaceAll("[\\{\\}\\-]","");
Also escaping the curly braces is not needed within character classes:
str = str.replaceAll("[{}-]","");
str.replaceAll("[{}-]","");