从字符串替换 \

问题描述

我想从下面的字符串中删除“”,为此我正在使用

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("\\","");

但这是在扔

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)

解决方法

如果您想删除字符串中的 " ,您可以替换 " 但您必须将其转义,因为它当然会关闭参数中的字符串。

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

或者更好地使用只是替换

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

因为 replaceAll 在内部使用了正则表达式,而您在这里不需要正则表达式。

你的字符串中的 \ " 不存在,它们只是用来转义 "