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?

asked Mar 24, 2013 at 3:16
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.

answered Mar 24, 2013 at 3:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.