PeakGen PeakGen - 6 months ago 32
Java Question

Removing First and Last Double Quotes

I have set of

String
s where the first and last characters are double quotes. Below is an example.

String x = "‘Gravity’ tops the box office for 3rd week | New York Post"


Some other strings will contain double quotes in the middle of the text, so I can't use
String.replaceAll()
. I just need to remove the first and last double quotes. How can I do this?

Answer

If the " characters are always going to be the first and last ones, you don't need a regex. Just use substring:

x = x.substring(1, x.length() - 1)
Comments