所有的转义字符都有哪些?

143

我知道Java中的一些转义字符,例如:

\n : Newline
\r : Carriage return
\t : Tab
\\ : Backslash
...

有没有一个完整的清单?


13
这在Java语言规范中有说明。 - skaffman
2
@user207421 虽然对于我们在谷歌搜索时找到它很好,但这并不是什么好事。 - jaredad7
5个回答

215
你可以在这里找到完整的列表:这里
  • \t 在此处插入一个制表符。
  • \b 在此处插入一个退格符。
  • \n 在此处插入一个换行符。
  • \r 在此处插入一个回车符。
  • \f 在此处插入一个换页符。
  • \s 在此处插入一个空格。
  • \' 在此处插入一个单引号。
  • \" 在此处插入一个双引号。
  • \\ 在此处插入一个反斜杠。

33
该列表中缺少Unicode和八进制转义字符:\u1234 \012 \01 \0。 - Sampo
5
以下是 Java 规范中关于 Unicode 转义序列的段落:链接 - JE42
9
在javac 1.8.0_20中,\a不能编译:“非法的转义字符:String test = "\a";” - Ehryk
3
“Unicode转义符在编译器运行之前被预处理。” -- Mark Peters。 因此,它们与此处列出的标准字符串转义不同。谢谢Jan在这个答案中的评论。 - Josiah Yoder
2
谈到Java 16时,这份列表几乎是不完整的。我猜想oracle的教程并没有定期更新。更好的选择是查看JLS。 - Qw3ry
显示剩余2条评论

50
Java Escape Sequences:

\u{0000-FFFF}  /* Unicode [Basic Multilingual Plane only, see below] hex value 
                  does not handle unicode values higher than 0xFFFF (65535),
                  the high surrogate has to be separate: \uD852\uDF62
                  Four hex characters only (no variable width) */
\b             /* \u0008: backspace (BS) */
\t             /* \u0009: horizontal tab (HT) */
\n             /* \u000a: linefeed (LF) */
\f             /* \u000c: form feed (FF) */
\r             /* \u000d: carriage return (CR) */
\"             /* \u0022: double quote (") */
\'             /* \u0027: single quote (') */
\\             /* \u005c: backslash (\) */
\{0-377}       /* \u0000 to \u00ff: from octal value 
                  1 to 3 octal digits (variable width) */

基本多语言平面是Unicode值从0x0000 - 0xFFFF(0-65535)的范围。其他平面只能通过多个字符在Java中指定:埃及象形文字A054(躺着的人)是U+1303F / 𓀿,需要将其拆分为"\uD80C\uDC3F"(UTF-16)以适用于Java字符串。其他一些语言支持更高的平面,使用"\U0001303F"


现有的答案没有涉及Java中的Unicode和八进制转义序列。 - Ehryk
3
\u000a 似乎无法工作 -> - 无效的字符常量在此处查看更多信息:https://dev59.com/GW865IYBdhLWcg3wU9GI - Jan
7
它正在工作,也许有些过于出色了。与\r\n不同,Unicode转义序列是在编译器运行之前进行预处理的,就像你链接到的问题所指定的那样。因此,它将一个字面上的换行符插入到您的代码中,并因此失败。但是,这个转义码按照规范预期的方式“工作”。 - Ehryk

1
此外,旧版本的Java还支持使用\v来表示换页符。
来自:java/util/regex/Pattern.java:
2600         case 'v':
2601             // '\v' was implemented as VT/0x0B in releases < 1.8 (though
2602             // undocumented). In JDK8 '\v' is specified as a predefined
2603             // character class for all vertical whitespace characters.
2604             // So [-1, root=VertWS node] pair is returned (instead of a
2605             // single 0x0B). This breaks the range if '\v' is used as
2606             // the start or end value, such as [\v-...] or [...-\v], in
2607             // which a single definite value (0x0B) is expected. For
2608             // compatibility concern '\013'/0x0B is returned if

0

是的,以下是docs.Oracle的链接,您可以在其中找到Java中转义字符的完整列表。

转义字符始终以"\"为前缀,并用于执行某些特定任务,例如换行等。

有关转义字符的更多详细信息,请参阅以下链接:

https://docs.oracle.com/javase/tutorial/java/data/characters.html


0

这些是转义字符,用于操作字符串。

\t  Insert a tab in the text at this point.
\b  Insert a backspace in the text at this point.
\n  Insert a newline in the text at this point.
\r  Insert a carriage return in the text at this point.
\f  Insert a form feed in the text at this point.
\'  Insert a single quote character in the text at this point.
\"  Insert a double quote character in the text at this point.
\\  Insert a backslash character in the text at this point.

从这里了解更多关于它们的信息。

http://docs.oracle.com/javase/tutorial/java/data/characters.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接