String s1=s.replace('"', '\"');
here i want to replace " with \"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.