我怎样才能让 Java 打印引号,比如“你好”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3844595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:45:44  来源:igfitidea点击:

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

javaescapingdouble-quotessystem.out

提问by Roy

How can I make Java print "Hello"?

如何进行 Java 打印"Hello"

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

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

采纳答案by Stephen C

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:

双引号字符必须在 Java 字符串文字中使用反斜杠进行转义。其他需要特殊处理的字符包括:

  • Carriage return and newline: "\r"and "\n"
  • Backslash: "\\\\"
  • Single quote: "\'"
  • Horizontal tab and form feed: "\t"and "\f"
  • 回车和换行:"\r""\n"
  • 反斜杠: "\\\\"
  • 单引号: "\'"
  • 水平标签和换页:"\t""\f"

The complete list of Java string and character literal escapes may be found in the section 3.10.6of the JLS.

Java 字符串和字符文字转义的完整列表可以在 JLS 的3.10.6 节中找到。

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.2and 3.3for a details on the use of Unicode in Java source code.

还值得注意的是,您可以使用“\uxxxx”形式的 Unicode 转义序列在源代码中包含任意 Unicode 字符,其中“x”是十六进制数字。但是,它们与普通的字符串和字符转义不同,因为您可以在 Java 程序的任何地方使用它们……而不仅仅是在字符串和字符文字中;见JLS部分3.13.23.3对在Java源代码使用Unicode的一个细节。

See also:

也可以看看:

回答by Nikita Rybak

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

转义字符串中的双引号: "\"Hello\""

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

有关该主题的更多信息(检查“转义序列”部分)

回答by keshav84

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

回答by Dhananjay

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

回答by sadananda salam

You can do it using a unicode character also

您也可以使用 unicode 字符来完成

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

回答by Logan

Use Escape sequence.

使用转义序列。

\"Hello\"

This will print "Hello".

这将打印“你好”。

回答by Atishay Jain

There are two easy methods:

有两种简单的方法:

  1. Use backslash \before double quotes.
  2. Use two single quotes instead of double quotes like ''instead of "
  1. \在双引号前使用反斜杠。
  2. 使用两个单引号代替双引号,例如 ''代替"

For example:

例如:

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

回答by Akmishra

char ch='"';

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

Or

或者

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

回答by user3185910

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"

你好”

回答by Mike Nakis

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