How can I make Java print quotes, like "Hello"?

JavaEscapingDouble Quotessystem.out

Java Problem Overview


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("").

Java Solutions


Solution 1 - Java

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:

Solution 2 - Java

char ch='"';

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

Or

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

           

Solution 3 - Java

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

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

Solution 4 - Java

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

Solution 5 - Java

You can do it using a unicode character also

System.out.print('\u0022' + "Hello" + '\u0022');

Solution 6 - Java

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();
}

Solution 7 - Java

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

Solution 8 - Java

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''"); 

Solution 9 - Java

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"

Solution 10 - Java

Use Escape sequence.

\"Hello\"

This will print "Hello".

Solution 11 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoyView Question on Stackoverflow
Solution 1 - JavaStephen CView Answer on Stackoverflow
Solution 2 - JavaAkmishraView Answer on Stackoverflow
Solution 3 - JavaNikita RybakView Answer on Stackoverflow
Solution 4 - JavaDhananjayView Answer on Stackoverflow
Solution 5 - Javasadananda salamView Answer on Stackoverflow
Solution 6 - JavaMike NakisView Answer on Stackoverflow
Solution 7 - Javakeshav84View Answer on Stackoverflow
Solution 8 - JavaAtishay JainView Answer on Stackoverflow
Solution 9 - Javauser3185910View Answer on Stackoverflow
Solution 10 - JavaLoganView Answer on Stackoverflow
Solution 11 - Javauser3033075View Answer on Stackoverflow