How to Enter Quotes in a Java String

How to enter quotes in a Java string?

In Java, you can escape quotes with \:

String value = " \"ROM\" ";

How can I make Java print quotes, like Hello?

System.out.print("\"Hello\"");

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: "\\"
  • Single quote: "\'"
  • Horizontal tab and form feed: "\t" and "\f"

The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.

It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx where the xs are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.

See also:

  • The Oracle Java Tutorial: Numbers and Strings - Characters

  • In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)

How to add quotes around printed String?

Because double quotes delimit String values, naturally you must escape them to code a literal double quote, however you can do it without escaping like this:

System.out.println('"' + s + '"');

Here, the double quote characters (") have been coded as char values. I find this style easier and cleaner to read than the "clumsy" backslashing approach. However, this approach may only be used when a single character constant is being appended, because a 'char' is (of course) exactly one character.

How to add double quotes around a String variable in Java?

There are actually 2 issues with your code.

  1. You need to make sure that \" is a String. This can be done by surrounding it with double quotes like this: "\""

  2. You need to concatenate Strings when printing them by using a +

Change the last line of code in your question to:

System.out.println("\"" + upperCasePhrase + "\"");

Quotation marks inside a string

String name = "\"john\"";

You have to escape the second pair of quotation marks using the \ character in front. It might be worth looking at this link, which explains in some detail.

Other scenario where you set variable:

String name2 = "\""+name+"\"";

Sequence in console:

> String name = "\"john\"";
> name
""john""
> String name2 = "\""+name+"\"";
> name2
"""john"""

Insert Double quotes as string

You have to put the escape character

else if(error.equals("Duplicate entry\"for key \"PRIMARY\""));

Putting a user's input in between double quotes

Try this

System.out.print("\"" + scanner + "\"" + "is not a valid command. Please try again : ");

This escapes the " by using \.

How can I assign a piece of text full of double quotes to a String variable in Java

Java 13 has a preview feature called Text Blocks which allow exactly* that:

String s = """
<your text here>
""";

You'll have to enable preview features for that and the feature may or may not change in future versions.

In earlier versions of Java there is no such functionality.

*Well, mostly. Text blocks are not really raw strings but allow double quotes at least.



Related Topics



Leave a reply



Submit