我如何使 Java 打印引号,如“ Hello”?

如何使 Java 打印 "Hello"

当我输入 System.out.print("Hello");时,输出将是 Hello。我要找的是带引号的 "Hello"("")。

605515 次浏览

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

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

System.out.println("\"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 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:

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

You can do it using a unicode character also

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

Use Escape sequence.

\"Hello\"

This will print "Hello".

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


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

Or

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

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"

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

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.