我有一个由反斜杠组成的字符串。
public static void main(String[] args) {
String str = "\"json_value\": \"{\\\"screen\\\":\\\"SCR-3\\\",\\\"price\\\":\\\"200\\\",\\\"count\\\":\\\"0\\\",\\\"name\\\":\\\"Regular Cup, Delishy 50 Ml\\\",\\\"seat_num\\\":\\\"D12\\\",\\\"image\\\":\\\"http://192.168.2.46:8080/OrderSnacks/JSON_images/icecream_cup_vanilla.jpg\\\",\\\"quantity\\\":\\\"2\\\",\\\"orderid\\\":\\\"14070738\\\",\\\"itemid\\\":\\\"57\\\",\\\"vendor_id\\\":\\\"10101500072001001\\\",\\\"date_time\\\":\\\"13:07:38\\\",\\\"toppings\\\":[{\\\"name\\\":\\\"Quantity 1\\\",\\\"value\\\":[\\\"Honey with Chocolate Sauce 10 ML\\\"]},{\\\"name\\\":\\\"Quantity 2\\\",\\\"value\\\":[\\\"Honey with Chocolate Sauce 10 ML\\\"]}]}\",";
str = str.replaceAll("\\\\", "\\\\\\\\");
System.out.println(str);
}
有人可以让我知道如何更换所有反向斜线吗?
我尝试使用str = str.replaceAll(“\\”,“\\\\”); ,但它没有取代它们。
答案 0 :(得分:1)
str = str.replaceAll("\\\\", "");
问题出在哪里???
答案 1 :(得分:1)
String str = "\"json_value\": \"{\\\"screen\\\":\\\"SCR-3\\\",\\\"price\\\":\\\"200\\\",\\\"count\\\":\\\"0\\\",\\\"name\\\":\\\"Regular Cup, Delishy 50 Ml\\\",\\\"seat_num\\\":\\\"D12\\\",\\\"image\\\":\\\"http://192.168.2.46:8080/OrderSnacks/JSON_images/icecream_cup_vanilla.jpg\\\",\\\"quantity\\\":\\\"2\\\",\\\"orderid\\\":\\\"14070738\\\",\\\"itemid\\\":\\\"57\\\",\\\"vendor_id\\\":\\\"10101500072001001\\\",\\\"date_time\\\":\\\"13:07:38\\\",\\\"toppings\\\":[{\\\"name\\\":\\\"Quantity 1\\\",\\\"value\\\":[\\\"Honey with Chocolate Sauce 10 ML\\\"]},{\\\"name\\\":\\\"Quantity 2\\\",\\\"value\\\":[\\\"Honey with Chocolate Sauce 10 ML\\\"]}]}\",";
str = str.replaceAll("\\\\", "");
System.out.println(str);
output:
"json_value": "{"screen":"SCR-3","price":"200","count":"0","name":"Regular Cup, Delishy 50 Ml","seat_num":"D12","image":"http://192.168.2.46:8080/OrderSnacks/JSON_images/icecream_cup_vanilla.jpg","quantity":"2","orderid":"14070738","itemid":"57","vendor_id":"10101500072001001","date_time":"13:07:38","toppings":[{"name":"Quantity 1","value":["Honey with Chocolate Sauce 10 ML"]},{"name":"Quantity 2","value":["Honey with Chocolate Sauce 10 ML"]}]}",
答案 2 :(得分:1)
由于replaceAll()
将第一个参数视为正则表达式,因此必须加倍才能转义反斜杠
str = str.replaceAll("\\\\", "");
\
是java中的一个特殊字符,在string.So中使用它时将\
视为普通字符,您需要放置另一个\
来关闭其在正则表达式中的特殊含义。因此,要在正则表达式中编写\\
,您需要使用四个\