如何制作 Java 打印引号,例如“Hello”?
如何让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在 Java 字符串文字中,双引号字符必须使用反斜杠进行转义。其他需要特殊处理的字符包括:
"\r"
和"\n"
"\\"
"\'"
"\t"
和"\f"
Java 字符串和字符的完整列表文字转义可以在 第 3.10.6 节中找到JLS 的。
还值得注意的是,您可以使用
\uxxxx
形式的 Unicode 转义序列在源代码中包含任意 Unicode 字符,其中x
是十六进制数字。然而,它们与普通的字符串和字符转义不同,因为您可以在 Java 程序中的任何地方使用它们……而不仅仅是在字符串和字符文字中;请参阅 JLS 部分 3.1,3.2 和 3.3 了解有关在 Java 源代码中使用 Unicode 的详细信息。另请参阅:
Oracle Java 教程:数字和字符串 - 字符< /a>
在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"
and"\n"
"\\"
"\'"
"\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 thex
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.See also:
The Oracle Java Tutorial: Numbers and Strings - Characters
In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)
或者
Or
转义字符串中的双引号:
"\"Hello\""
有关该主题的更多信息(检查“转义序列”部分)
Escape double-quotes in your string:
"\"Hello\""
More on the topic (check 'Escape Sequences' part)
您也可以使用 unicode 字符来完成此操作
You can do it using a unicode character also
添加实际的引号字符只是问题的一小部分;一旦你这样做了,你可能会面临真正的问题:如果字符串已经包含引号、换行符或其他不可打印的字符,会发生什么?
以下方法将处理一切:
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:
有两种简单的方法:
\
。''
而不是"
例如:
There are two easy methods:
\
before double quotes.''
instead of"
For example:
请注意,使用特定字符运行反斜杠时需要注意一些特定事项。
上面的输出将是:
Hello\
上面的输出将是:
Hello"
Take note, there are a few certain things to take note when running backslashes with specific characters.
The output above will be:
Hello\
The output above will be:
Hello"
使用转义序列。
这将打印“Hello”。
Use Escape sequence.
This will print "Hello".
您可以使用 json 序列化工具来引用 java 字符串。
像这样:
如果输入是:
hello
输出将是:"hello"
如果你想自己实现该函数:
它可能是这样的:
上面的实现将引用 escape字符串中的字符但排除
开头和结尾处的
"
。上面的实现并不完整。如果您需要其他转义字符,可以添加。
you can use json serialization utils to quote a java String.
like this:
if input is:
hello
output will be:"hello"
if you want to implement the function by self:
it maybe like this:
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.