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 + "\"");
How to print double quote in java ?? what should be syntax?
String expectedMsg2 = "test: \""+A1+ "\" is mapped to Product. Cannot delete";
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 \
.
Is there an alternative way to generate quote marks without using the escape sequence?
Have you tried this:
\"example text\"
So you would have something like this:
System.out.println("Replaced \"BFF\" with " + "\"" + BFF + "\"");
or
System.out.println("Replaced \"BFF\" with \"" + BFF + "\"");
Java: How to print quotations with a string method?
Replace
ifYoureHappyVerse("shout \"Hurray!\"", "(hoo-ray! hoo-ray!);
to
ifYoureHappyVerse("shout \"Hurray!\"", "(hoo-ray! hoo-ray!)");
Output
If you're happy and you know it, shout "Hurray!"(hoo-ray! hoo-ray!)
Related Topics
How to Move My Jmenubar to the Screen Menu Bar on MAC Os X
How to Mark Jtable Cell Input as Invalid
Java Simpledateformat Always Returning January for Month
Calling One Jframe from Another Using Timer Without Any Buttons
How Would You Implement an Lru Cache in Java
How to Convert a Char Array Back to a String
Running Code After Spring Boot Starts
Getting the Filenames of All Files in a Folder
Java 8: Lambda-Streams, Filter by Method with Exception
How to Hash Some String with Sha256 in Java
Is There a Java API That Can Create Rich Word Documents
How to Randomize Two Arraylists in the Same Fashion
How to Add an Actionlistener Onto a Jbutton in Java
Date and Time Conversion to Some Other Timezone in Java