0

I want to remove "" from the below string, and for that I am using

String orderDetailsDb =  "[{\"productId\":\"2814ff15-2fcf-4dc8-bf90-032e77a73e6b \",\"productName\":\"Garlic Oil 30 Capsules\",\"quantity\":6,\"price\":63.06,\"discount\":0,\"discountedPrice\":63.06,\"rowPrice\":\"\",\"gtinCode\":\"9324917000603\",\"accountNumber\":\"50138\",\"isPromo\":\"\"}]"

orderDetailsDb.replaceAll("\\", "");

but this is throwing

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
        at java.util.regex.Pattern.error(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
        at java.util.regex.Pattern.<init>(Unknown Source)
        at java.util.regex.Pattern.compile(Unknown Source)
3
  • 1
    What are the square brackets around the string literal? That's not valid Java. Commented May 11, 2021 at 6:44
  • 1
    Those backslashes aren't really there, they are just visible to escape the double quotes inside a Java string literal. Commented May 11, 2021 at 6:45
  • And "\\" doesn't represent a valid Java regex. Commented May 11, 2021 at 6:46

1 Answer 1

1

If you want to remove the " in your string you can replace the " but you have to escape it because of course it would close the string in your argument.

orderDetailsDb.replaceAll("\"", "")

or better use just replace

orderDetailsDb.replace("\"", "")

because replaceAll uses regex internally and you don't need a regex expression here.

the \ " in your strings are not there, they are just used to escape the "

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.