For example:
"I don't like these "double" quotes"
and I want the output to be
I don't like these double quotes
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: "\\"
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.
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.
Double quote without any character means empty String in Java. If you concat multiple empty String values result will again be an empty String .
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("\"", ""));
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("\"", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With