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 x
s 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.
You need to make sure that \" is a String. This can be done by surrounding it with double quotes like this:
"\""
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
What Are All the Escape Characters
Properly Removing an Integer from a List≪Integer≫
Java.Sql.Sqlexception Parameter Index Out of Range (1 ≫ Number of Parameters, Which Is 0)
How to Convert Milliseconds to "X Mins, X Seconds" in Java
How to Set the Proxy to Be Used by the Jvm
Waiting For Multiple Swingworkers
Java Security: Illegal Key Size or Default Parameters
Why Won't Eclipse Oxygen Start (For the First Time)
Is There an Advantage to Use a Synchronized Method Instead of a Synchronized Block
Creating an Instance Using the Class Name and Calling Constructor
Split String to Equal Length Substrings in Java
Java Error: Comparison Method Violates Its General Contract
In Arrayblockingqueue, Why Copy Final Member Field into Local Final Variable
Bringing Jfilechooser on Top of All Windows
When Should I Use "This" in a Class
Persistentobjectexception: Detached Entity Passed to Persist Thrown by JPA and Hibernate