【问题标题】:What are all the escape characters?所有的转义字符是什么?
【发布时间】:2009-09-02 12:10:46
【问题描述】:

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

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

某处有完整的清单吗?

【问题讨论】:

  • 不要在 Internet 上询问您可以轻松或更轻松地自行查找的问题。你冒着严重错误的风险。
  • @user207421 虽然很高兴我们其他人在谷歌搜索时找到它。

标签: java string escaping


【解决方案1】:

您可以找到完整列表here。

  • \t此时在文本中插入一个制表符。
  • \b此时在文本中插入一个退格键。
  • \n此时在文本中插入换行符。
  • \r此时在文本中插入回车。
  • \f 此时在文本中插入换页符。
  • \'此时在文本中插入一个单引号字符。
  • \"此时在文本中插入双引号字符。
  • \\此时在文本中插入一个反斜杠字符。

【讨论】:

  • 列表缺少 Unicode 和八进制转义符:\u1234 \012 \01 \0
  • \a 在 javac 1.8.0_20 中无法编译:illegal escape character: String test = "\a";
  • "Unicode 转义在编译器运行前被预处理。" --Mark Peters。因此它们与此处列出的标准字符串转义不同。感谢 Jan 对 this answer 的评论
  • 说到 Java 16,这个列表几乎是完整的。我认为 oracle 教程不会定期更新。你最好的选择是检查JLS
【解决方案2】:
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) */

Basic Multilingual Plane 是 0x0000 - 0xFFFF (0 - 65535) 之间的 unicode 值。其他平面只能在 Java 中由多个字符指定:埃及象形文字 A054(放下花花公子)是 U+1303F / 𓀿,对于 Java 字符串,必须分解为 "\uD80C\uDC3F" (UTF-16)。其他一些语言使用"\U0001303F" 支持更高的平面。

【讨论】:

  • 现有答案未解决 Java 中的 unicode 和八进制转义序列。
  • \u000a 似乎不起作用 -> - invalid character constant 查看更多 here
  • @Jan 它工作正常,也许太好了。例如,与\r 和\n 不同,unicode 转义在编译器运行之前按照您链接到的问题指定的问题进行预处理。因此,它会在您的代码中插入文字换行符并因此而失败。但是,转义码在规范中是“有效的”。
【解决方案3】:

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

转义字符总是以“\”开头并用于执行某些特定任务,例如转到下一行等。

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

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

【讨论】:

    【解决方案4】:

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

    \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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 2020-06-24
      相关资源
      最近更新 更多