4

Why doesn't this line of code work? It's supposed to replace all the punctuations with nothing.

m = m.replace("[\\?\\.;:'\"]", "")

m will not change after executing this line in java even though it contains punctuations. Why?

1
  • 2
    Dasblinkenlight nailed it. As a side note, there's no need to escape a question mark or period inside a character class. Commented Mar 24, 2013 at 3:27

1 Answer 1

16

This is because replace takes a String; you need to call replaceAll, which does take a regular expression.

Sign up to request clarification or add additional context in comments.

Comments

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.