Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you replace double quotes with a blank space in Java?

For example:

"I don't like these "double" quotes" 

and I want the output to be

I don't like these double quotes 
like image 728
angad Soni Avatar asked Mar 02 '10 00:03

angad Soni


People also ask

How do you escape quotes in Java?

The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include: Carriage return and newline: "\r" and "\n" Backslash: "\\"

How do you remove double quotes from a replacement string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How do you escape a double quote?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

Can we use empty double quotes in Java?

Double quote without any character means empty String in Java. If you concat multiple empty String values result will again be an empty String .


2 Answers

Use String#replace().

To replace them with spaces (as per your question title):

System.out.println("I don't like these \"double\" quotes".replace("\"", " ")); 

The above can also be done with characters:

System.out.println("I don't like these \"double\" quotes".replace('"', ' ')); 

To remove them (as per your example):

System.out.println("I don't like these \"double\" quotes".replace("\"", "")); 
like image 77
cletus Avatar answered Sep 16 '22 20:09

cletus


You don't need regex for this. Just a character-by-character replace is sufficient. You can use String#replace() for this.

String replaced = original.replace("\"", " "); 

Note that you can also use an empty string "" instead to replace with. Else the spaces would double up.

String replaced = original.replace("\"", ""); 
like image 35
BalusC Avatar answered Sep 18 '22 20:09

BalusC