简体   繁体   中英

How to replace char inside double quotes in android

In my Android app, I want to change the value of a String between double quotes. For example, I want to replace {"txt": with {"":

I have tried the following regex expressions, but they don't work...

    String abc=replace(str, "{\\txt\\:", "");
    String abc=replace(str, "{'txt':", "");...,etc

Could anyone please offer help with this.

You need to escape each double-quote using a backslash.

Your question is a little ambiguous, so I'll cover the two most reasonable interpretations of it:


If you want to replace {"txt": with "" (as stated in your question) then use this:

String abc = str.replace("{\"txt\":", "\"\"");

With this code the text {"txt":foo} becomes ""foo} .


If you want to replace {"txt": with the empty string (as implied by your example code) then use this:

String abc = str.replace("{\"txt\":", "");

With this code the text {"txt":foo} becomes foo} .

"{\\\\txt\\\\:"表示字符串{\\txt\\ ,如果要匹配(并因此替换) {"txt"使用"{\\"txt\\":"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM