92

How can I make Java print "Hello"?

When I type System.out.print("Hello"); the output will be Hello. What I am looking for is "Hello" with the quotes("").

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Roy
  • 967
  • 2
  • 8
  • 7
  • 2
    possible duplicate of [In Java, is there a way to write a string literal without having to escape quotes?](http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote) – Bucket Jan 16 '14 at 17:57
  • 2
    Possible duplicate of [How to enter quotes in a Java string?](http://stackoverflow.com/questions/3559063/how-to-enter-quotes-in-a-java-string) – fabian May 27 '16 at 14:12

11 Answers11

166
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:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
11
char ch='"';

System.out.println(ch + "String" + ch);

Or

System.out.println('"' + "ASHISH" + '"');
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
Akmishra
  • 119
  • 1
  • 3
7

Escape double-quotes in your string: "\"Hello\""

More on the topic (check 'Escape Sequences' part)

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
4

Adding the actual quote characters is only a tiny fraction of the problem; once you have done that, you are likely to face the real problem: what happens if the string already contains quotes, or line feeds, or other unprintable 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();
}
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • If you do need to do this, a (usually) better alternative to this is to use an existing library method (e.g. Apache Commons) to do the escaping / de-escaping. Or make use of an API's ability to do this; e.g. JDBC `PreparedStatement`, the `URL` and `URI` constructors. – Stephen C May 25 '18 at 23:44
  • @StephenC yes, of course. The usefulness of this answer just lies in the complement of the "(usually)" that you mentioned. Plus the code may have some educational value. – Mike Nakis May 26 '18 at 04:04
4
System.out.println("\"Hello\""); 
TylerH
  • 20,799
  • 66
  • 75
  • 101
Dhananjay
  • 734
  • 1
  • 9
  • 14
4

You can do it using a unicode character also

System.out.print('\u0022' + "Hello" + '\u0022');
mdml
  • 22,442
  • 8
  • 58
  • 66
sadananda salam
  • 845
  • 1
  • 7
  • 12
2
System.out.println("\"Hello\"")
mdml
  • 22,442
  • 8
  • 58
  • 66
keshav84
  • 2,291
  • 5
  • 25
  • 34
1

There are two easy methods:

  1. Use backslash \ before double quotes.
  2. Use two single quotes instead of double quotes like '' instead of "

For example:

System.out.println("\"Hello\"");                       
System.out.println("''Hello''"); 
asteri
  • 11,402
  • 13
  • 60
  • 84
  • 4
    Erm ... the 2nd approach is not solving the problem. It is changing the problem. Two single quotes does not mean the same thing as a double quote ... to a computer, or to a knowledgeable person. – Stephen C Nov 30 '15 at 03:30
1

Take note, there are a few certain things to take note when running backslashes with specific characters.

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

The output above will be:

Hello\


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

The output above will be:

Hello"

TylerH
  • 20,799
  • 66
  • 75
  • 101
0

Use Escape sequence.

\"Hello\"

This will print "Hello".

Logan
  • 2,445
  • 4
  • 36
  • 56
0

you can use json serialization utils to quote a java String.

like this:

public class Test{
   public static String quote(String a){
       return JSON.toJsonString(a)
   } 
}

if input is:hello output will be: "hello"

if you want to implement the function by self:

it maybe like this:

public static String quotes(String origin) {

        // 所有的 \ -> \\ 用正则表达为: \\ => \\\\" 再用双引号quote起来: \\\\ ==> \\\\\\\\"
        origin = origin.replaceAll("\\\\", "\\\\\\\\");
        //  " -> \" regExt: \" => \\\" quote to param: \\\" ==> \\\\\\\"
        origin = origin.replaceAll("\"", "\\\\\\\"");

        // carriage return: -> \n \\\n
        origin = origin.replaceAll("\\n", "\\\\\\n");

        // tab -> \t
        origin = origin.replaceAll("\\t", "\\\\\\t");
        return origin;
    }

the above implementation will quote escape character in string but exclude the " at the start and end.

the above implementation is incomplete. if other escape character you need , you can add to it.

user3033075
  • 139
  • 1
  • 6