Search code examples
javastringreplaceall

how to replace ' with " inside a String


I have a String containing: 'abc' 'abc' 'abc'. How can i use replaceAll, to produce: "abc" "abc" "abc" ?

I tried using

StringA=StringA.replaceAll(''','"');

Solution

  • The method to replace every occurrence of a char by another char is replace().

    The char literal for a single quote is '\'' (the single quote must be escaped, so that it's not interpreted as the end of the char literal).

    So you want

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

    replaceAll(), suggested by many other answers, replaces substrings matching a regexp by another substring. It's less appropriate than the method replacing a single char by another one.

    Side note: please respect the Java naming conventions. Variables start with a lowercase letter. Only class names start with an uppercase letter.