简体   繁体   English

转义序列的不一致行为

[英]Escape Sequence's inconsistent behavior

I am curious as to why escape sequencing evaluates to different results in different situations.我很好奇为什么转义序列在不同情况下会评估为不同的结果。

public class EscapeChars{

    public static void main(String args[]) {
        String sequence = args[0];
        System.out.println(sequence);
    }
}

Input "//\\\\" Output //\\ - This is expected, given that the sequence "\\\\" evaluates to \\输入"//\\\\"输出//\\ - 这是预期的,因为序列"\\\\"计算为\\

Input "\\\\//" Output \\\\// - Going by my understanding of escape sequencing, I expected the output to be \\// instead.输入"\\\\//"输出\\\\// - 根据我对转义序列的理解,我希望输出为\\//

I want to understand the reason for this apparent inconsistent result.我想了解这种明显不一致结果的原因。

If you try this code:如果您尝试此代码:

System.out.println("\/");

You will get a compilation error( I tested with jdk 1.8 ).你会得到一个编译错误(我用 jdk 1.8 测试过)。 The error says: illegal escape character.错误说:非法转义字符。 Which means "/" is invalid.这意味着“/”无效。 Since you are input the sequence at runtime, so this may be the reason of showing the result as it was inputted.由于您是在运行时输入序列,所以这可能是显示输入结果的原因。

据我所知,你的 java 程序只是打印由 shell 传递给它的参数,或者任何执行它的程序,所以在打印时,转义序列已经被 bean 处理,所以它不是一个爪哇问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM