How to escape double quotes in String in java

Escape double quotes in String in Java

In this post, we will see how to escape double quotes in String in java.

There are scenarios where you need to escape double quotes already present in the String. This generally happens while dealing with JSON file format or reading file data.

Escape double quotes in java

Double quotes characters can be escaped with backslash(\) in java. You can find complete list of String escapes over this java doc.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Let’s understand this with the help of example.

Output:

BlogName: Java2blog is java blog
BlogName with double quotes: “Java2blog” is java blog

💡 Did you know?

Some IDEs like intelij automatically escape String if you paste the String between double quotes surrounding the String literal.

Add double quotes to String in java

If you want to add double quotes(") to String, then you can use String’s replace() method to replace double quote(") with double quote preceded by backslash(\").

Here is an example.

Output:

BlogName: Java2blog is java blog
BlogName with double quotes: “Java2blog” is java blog

Print String with double quotes in java

If you want to print String with double quotes in java using System.out.println, then you can use either \" or ‘"’ with the String.

Here is an example.

Output:

“Java2blog”
“Java2blog”
“Java2blog”

That’s all about How to escape double quotes in String in java.

Related Posts

  • 15 April

    How to declare a String array in java

    Learn about how to declare String array in java.

  • 24 December

    How to capitalize first letter in java

    Learn about how to capitalize first letter in java.

  • 06 December

    Difference between replace() and replaceAll() in java

    In this post, we will see difference between String’s replace() and replaceAll() methods in java. String’s replace() and replaceAll() both replace all occurences in the String. String’s replace() takes either two chars or two CharSequences as arguments and it will replace all occurrences of char or String but replaceAll() method takes regex String as argument […]

  • 24 April

    Add character to String in java

    In this post, we will see how to add character to String in java. There are multiple ways to add character to String. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Add character to the start of String You […]

  • 24 March

    Initialize List of String in java

    In this post, we will see how to initialize List of String in java. Can you initialize List of String as below: [crayon-609221b892c46116868137/] You can't because List is an interface and it can not be instantiated with new List(). 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for […]

  • 29 February

    Java long to String

    In this post, we will see how to convert long to String in java. There are lot of ways to convert long to String.Let’s see each one by one. Using Long.toString() You can use Long class toString() method to convert long to String. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.