String s1=s.replace('"', '\"');
here i want to replace "
with \"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With