How to print double quotes in Java

In this post we will provide an example of how to print double quotes in Java. This is useful when you want to print JSON  or just want to add quotes to a name or word.

How to print double quotes in Java

The first example adds single quotes to a String. In this example no special characters are required.

Running the above produces the following:

To add double quotes to a String you will need to include a escape sequence which has a special meaning to the compiler.  The table below shows the escape sequences available in Java

Escape Sequence Description
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\' Insert a single quote character in the text at this point.
\" Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

Using the above as reference, we need to use \"

Running the above code snippet produces the following:

How to remove double quotes from a String

To remove double quotes from a String you can use the replace() method on the String class. As you can see in the example below we have replaced the " with nothing hence removing the double quotes.

Running the above example produces the following:

Similar posts

  1. How to reverse a String in Java
  2. How to split a String by new line
  3. How to loop through a list in Java
  4. Check if an Array contains a certain value

References

  1. Java special characters

Leave a Reply