The .replace string does not work in Android

String s1=s.replace('"', '\"'); 

here I want to replace " with \"

+4
source share
2 answers

Try String s1 = s.replace("\"", "\\\"");

Explanation:
When referencing a quote or backslash in a string, i.e. Anything inside double quotation marks requires \ to indicate that you want the quote to appear in quotation marks rather than ending in quotation marks. Does this make sense?

For example, you should write String message = "She said \"Hello\" the other day." , so backslashes mean that quotation marks do not actually end the entire string, but rather are part of the string.

+17
source
 String s1=s.replace("\"", "\\\""); 

It will replace everything " with \" .

+2
source

Source: https://habr.com/ru/post/1415574/


All Articles