如何制作Java打印引号,例如“Hello”? [英] How can I make Java print quotes, like "Hello"?

查看:125
本文介绍了如何制作Java打印引号,例如“Hello”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作Java打印Hello

How can I make Java print "Hello"?

当我输入 System.out.print(Hello); 输出将是 Hello 。我要找的是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\"");

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

The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:


  • 回车和换行:\ r\ n

  • 反斜杠:\\\\

  • 单引号:\'

  • 水平制表符和换页符:\ t\ f

  • Carriage return and newline: "\r" and "\n"
  • Backslash: "\\\\"
  • Single quote: "\'"
  • Horizontal tab and form feed: "\t" and "\f"

可以在第3.10.6节

它是另外值得注意的是,您可以使用\uxxxx形式的Unicode转义序列在源代码中包含任意Unicode字符,其中x是十六进制数字。但是,这些与普通的字符串和字符转义不同,因为您可以在Java程序中的任何位置使用它们...而不仅仅是字符串和字符文字;请参阅JLS部分 3.1 3.2 3.3

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

参见:

http://stackoverflow.com/questions/3034186/in-java-is-there-a-way-to-write-a-string-literal-without-having-to-escape-quote (答案:否)

这篇关于如何制作Java打印引号,例如“Hello”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆