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?
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("[{}-]","");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.