replace a string which already contains double quotes with blank

649 Views Asked by At

I have the following string with me which came from an XML and saved in String str: "welcome "to" the world"

Now I want to replace it completely with an empty string

I have tried

str.replaceAll("welcome "to" the world", ""));

but no luck. Can anyone please guide me?

edited guys as I said earlier the string comes from an XML and gets saved in a String , So in this case I can't add the escape characters to it.

here you go. This is xml that i get

now i store it in a string and want to replace the top line ie. with blank so that I can render it to another place.

hope this helps. Thankyou :)

3

There are 3 best solutions below

1
On

You have to escape double quotes " with a backslash \"

public static void main(String[] args) {
        String s = "\"welcome \"to\" the world\"";
        s = s.replace("\"welcome \"to\" the world\"", "");
        System.out.println(s);

    }

or you can use

 s = s.replaceAll("\"welcome \"to\" the world\"", "");

Output

0
On

Double Quotes inside a double quote requires escape sequence.

    String in="this replace \"sy\" this is my string";
   System.out.println(in.replaceAll("replace \"sy\" this",""));
0
On

You have to escape the quotes within the string with a backslash: \

String str = "\"welcome \"to\" the world\"";
str = str.replaceAll("\"welcome \"to\" the world\"", "");
System.out.println(str);