为什么我不能在 Java 中使用 u000D 和 u000A 作为 CR 和 LF? [英] Why can't I use u000D and u000A as CR and LF in Java?

查看:19
本文介绍了为什么我不能在 Java 中使用 u000D 和 u000A 作为 CR 和 LF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

String x = "u000A hello";//Error - Illegal escape character in string literal.

推荐答案

Unicode 转义符在编译器运行之前进行了预处理.因此,如果你把 u000A 放在像这样的字符串文字中:

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

String someString = "foou000Abar";

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

It will be compiled exactly as if you wrote:

String someString = "foo
bar";

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

Stick to (carriage return; 0x0D) and (line feed; 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: u000au007du007b
            System.out.println("false is true!");
        }
    }
}

这篇关于为什么我不能在 Java 中使用 u000D 和 u000A 作为 CR 和 LF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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