Java - Write code to escape Quotes in a string

Requirements

Write code to escape Quotes

\ becomes \\.

Hint

You can use the replace method to escape string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com\"";
        System.out.println(escapeQuotes(s));
    }// w w w. j a v a  2s  .c  o m

    public static String escapeQuotes(String s) {
        s = s.replace("\\", "\\\\");
        s = s.replace("\"", "\\\"");

        return s;
    }
}

Related Exercise