为什么我不能在 Java 中使用 \u000D 和 \u000A 作为 CR 和 LF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3866187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:01:20  来源:igfitidea点击:

Why can't I use \u000D and \u000A as CR and LF in Java?

javaunicode

提问by sadananda salam

Why can't I use \u000D and \u000A as CR and LF in Java? It's giving an error when I compile the code:

为什么我不能在 Java 中使用 \u000D 和 \u000A 作为 CR 和 LF?编译代码时报错:

illegal line end in character literal

采纳答案by Mark Peters

Unicode escapes are pre-processed before the compiler is run. Therefore, if you put \u000Ain a String literal like this:

在运行编译器之前对 Unicode 转义进行预处理。因此,如果您\u000A输入这样的字符串文字:

String someString = "foo\u000Abar";

It will be compiled exactly as if you wrote:

它将完全按照您编写的方式进行编译:

String someString = "foo
bar";

Stick to \r(carriage return; 0x0D) and \n(line feed; 0x0A)

坚持\r(回车;0x0D)和\n(换行;0x0A

Bonus:You can always have fun with this, especially given the limitations on most syntax highlighters. Next time you've got a sec, try running this code:

奖励:您总是可以从中获得乐趣,尤其是考虑到大多数语法荧光笔的局限性。下次有时间时,请尝试运行以下代码:

public class FalseIsTrue {
    public static void main(String[] args) {
        if ( false == true ) { //these characters are magic: \u000a\u007d\u007b
            System.out.println("false is true!");
        }
    }
}

回答by Matas Vaitkevicius

Because it falls within the range of Unicode Control characters

因为它属于Unicode控制字符的范围

Which is U+0000–U+001Fand U+007F.

这是U+0000–U+001FU+007F

Unicode control characters are used to control the interpretation or display of text, but these characters themselves have no visual or spatial representation.

Unicode 控制字符用于控制文本的解释或显示,但这些字符本身没有视觉或空间表示。

They can be escaped by using \like described in above answer by @Mark

他们可以通过使用\@Mark上面的回答中描述的方法来转义

FROM RFC:

RFC

2.5. Strings

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters(U+0000 through U+001F).

Any character maybe escaped.

2.5. 字符串

字符串的表示类似于 C 系列编程语言中使用的约定。字符串以引号开始和结束。除了必须转义的字符外,所有 Unicode 字符都可以放在引号内:引号、反斜杠和控制字符(U+0000 到 U+001F)。

任何字符可以转义。