Reputation: 3819
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?
Upvotes: 1
Views: 53
Reputation: 72884
String
s 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("[{}-]","");
Upvotes: 2