如何制作 Java 打印引号,例如“Hello”?

发布于 2024-09-25 23:31:24 字数 171 浏览 8 评论 0原文

如何让 Java 打印 "Hello"

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

手心的海 2024-10-02 23:31:24
System.out.print("\"Hello\"");

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

  • 回车符和换行符:"\r""\n"
  • 反斜杠:"\\"
  • Single quote: "\'"
  • 水平制表符和换页符:"\t""\f"

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

还值得注意的是,您可以使用 \uxxxx 形式的 Unicode 转义序列在源代码中包含任意 Unicode 字符,其中 x 是十六进制数字。然而,它们与普通的字符串和字符转义不同,因为您可以在 Java 程序中的任何地方使用它们……而不仅仅是在字符串和字符文字中;请参阅 JLS 部分 3.13.23.3 了解有关在 Java 源代码中使用 Unicode 的详细信息。

另请参阅:

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:

弄潮 2024-10-02 23:31:24
char ch='"';

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

或者

System.out.println('"' + "ASHISH" + '"');
char ch='"';

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

Or

System.out.println('"' + "ASHISH" + '"');
夏末染殇 2024-10-02 23:31:24

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

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

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

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

我是男神闪亮亮 2024-10-02 23:31:24

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

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

You can do it using a unicode character also

System.out.print('\u0022' + "Hello" + '\u0022');
撩人痒 2024-10-02 23:31:24

添加实际的引号字符只是问题的一小部分;一旦你这样做了,你可能会面临真正的问题:如果字符串已经包含引号、换行符或其他不可打印的字符,会发生什么?

以下方法将处理一切:

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

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();
}
孤单情人 2024-10-02 23:31:24
System.out.println("\"Hello\""); 
System.out.println("\"Hello\""); 
口干舌燥 2024-10-02 23:31:24
System.out.println("\"Hello\"")
System.out.println("\"Hello\"")
芯好空 2024-10-02 23:31:24

有两种简单的方法:

  1. 在双引号前使用反斜杠 \
  2. 使用两个单引号而不是双引号,例如 '' 而不是 "

例如:

System.out.println("\"Hello\"");                       
System.out.println("''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''"); 
秋风の叶未落 2024-10-02 23:31:24

请注意,使用特定字符运行反斜杠时需要注意一些特定事项。

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

上面的输出将是:

Hello\


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

上面的输出将是:

Hello"

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"

您的好友蓝忘机已上羡 2024-10-02 23:31:24

使用转义序列。

\"Hello\"

这将打印“Hello”。

Use Escape sequence.

\"Hello\"

This will print "Hello".

戏蝶舞 2024-10-02 23:31:24

您可以使用 json 序列化工具来引用 java 字符串。

像这样:

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

如果输入是:hello 输出将是:"hello"

如果你想自己实现该函数:

它可能是这样的:

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

上面的实现将引用 escape字符串中的字符但排除
开头和结尾处的"

上面的实现并不完整。如果您需要其他转义字符,可以添加。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文