Johan
Johan

Reputation: 3819

Stripping chars from a String with java.regex

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

Answers (1)

M A
M A

Reputation: 72884

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("[{}-]","");

Upvotes: 2

Related Questions