Escape double quotes in Java String

There are some cases when we need to have double quotes as part of the String. In this post, you will see how to escape double quotes in Java String using escape character (/).

Escaping double quotes in Java String

If we try to add double quotes inside a String, we will get a compile-time error. To avoid this, we just need to add a backslash (/) before the double quote character.

In this example, we want to print double quotes around the words “Steve” and “Java”.

class EscapeDoubleQuotesInString {

  public static void main(String[] args) {

    String str = "Hello, I'm \"Steve\" and I am \"Java\" developer";


Output: Hello, I’m “Steve” and I am “Java” developer
That’s it!

Leave a Reply

Your email address will not be published.