1

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?

1
  • 1
    how your input string looks like? Note that strings are immutable in java, you need to assign the result back to another variable. Commented Mar 31, 2015 at 17:25

1 Answer 1

2

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("[{}-]","");
Sign up to request clarification or add additional context in comments.

1 Comment

more simpler str.replaceAll("[{}-]","");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.