Kalai Selvan.G
Kalai Selvan.G

Reputation: 482

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.

Upvotes: 0

Views: 732

Answers (2)

Mark Byers
Mark Byers

Reputation: 838716

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}.

Upvotes: 5

Thomas
Thomas

Reputation: 88737

"{\\txt\\:" would mean the string {\txt\, if you want to match (and thus replace) {"txt" use "{\"txt\":"

Upvotes: 1

Related Questions