How can I make Java print quotes like Hello?

How can I print Java "Hello" ?

When I type System.out.print("Hello"); , the output will be Hello . I am looking for "Hello" with quotation marks ( "" ).

+60
java escaping double-quotes
02 Oct 2018-10-10T00:
source share
13 answers
 System.out.print("\"Hello\""); 

The double quote character must be escaped with a backslash in the Java string literal. Other characters in need of special treatment include:

  • Carriage return and new line: "\r" and "\n"
  • Backslash: "\\\\"
  • Single quote: "\'"
  • Horizontal tab and feed form: "\t" and "\f"

For a complete list of Java strings and character literals, see JLS Section 3.10.6 .

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

See also:

+113
02 Oct. '10 at 6:08
source share
 char ch='"'; System.out.println(ch + "String" + ch); 

Or

 System.out.println('"' + "ASHISH" + '"'); 
+7
Sep 06 '14 at 9:53 on
source share

Reset double quotes in your string: "\"Hello\""

More on the topic (check the "Evacuation sequences" part)

+6
Oct 02 '10 at 6:08
source share

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

+4
Oct 02 2018-10-10T00:
source share

You can do this using the unicode character.

 System.out.print('\u0022' + "Hello" + '\u0022'); 
+3
Oct 02 '10 at 6:26
source share

Adding actual quotation marks is only a small part of the problem; as soon as you do this, you are likely to run into a real problem: what happens if the line already contains quotation marks or lines or other non-printable characters?

The following method will take care of everything:

 public static String escapeForJava( String value, boolean quote ) { StringBuilder builder = new StringBuilder(); if( quote ) builder.append( "\"" ); for( char c : value.toCharArray() ) { if( c == '\'' ) builder.append( "\\'" ); else if ( c == '\"' ) builder.append( "\\\"" ); else if( c == '\r' ) builder.append( "\\r" ); else if( c == '\n' ) builder.append( "\\n" ); else if( c == '\t' ) builder.append( "\\t" ); else if( c < 32 || c >= 127 ) builder.append( String.format( "\\u%04x", (int)c ) ); else builder.append( c ); } if( quote ) builder.append( "\"" ); return builder.toString(); } 
+3
Mar 16 '15 at 8:33
source share
 System.out.print("\"Hello\""); 

Are you looking for Escape sequences

Details List of Escape Sequences

+3
Mar 07 '17 at 23:16
source share
 System.out.println("\"Hello\"") 
+2
02 Oct '10 at 6:08
source share

There are two simple methods:

  • Use backslash \ in front of double quotes.
  • Use two single quotes instead of double quotes, such as '' instead of "

For example:

 System.out.println("\"Hello\""); System.out.println("''Hello''"); 
+2
Jun 20 '13 at 15:13
source share

Use the escape sequence.

 \"Hello\" 

This will print "Hello."

+1
Oct 02 2018-10-10
source share

Please note: there are several definitions to consider when performing backslashes with specific characters.

System.out.println ("Hello \\"); * edited file The above result will be:

Hello \

System.out.println ("Hello \" "); The above result will be:

Hello "

0
Jan 17 '15 at 6:26
source share
 android:text="import java.IO.*; \npublic class Hello \n { \nSystem.out.println(\&quot;Hello World\&quot;); \n}" 
-3
May 26 '15 at 12:26
source share

This is probably the best answer you will find:

 while(True){ System.out.println("\"Hello\""); } 
-6
Jan 16 '14 at 17:53
source share



All Articles