Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String .replace is not working in Android

Tags:

string

android

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

here i want to replace " with \"

like image 274
Bharathi D Avatar asked Dec 01 '22 23:12

Bharathi D


1 Answers

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

Explanation:
When referencing a quote or backslash in a string, i.e. anything inside double quotes, a \ is required to state that you want the quote to appear within the quotes, not end the quotes. Does this make sense?

For example, you would write String message = "She said \"Hello\" the other day.", so that the backslashes represent that the quotes don't actually end the whole string, but are rather to be part of the string.

like image 185
Mxyk Avatar answered Dec 05 '22 04:12

Mxyk