命令行参数中的转义序列(Java) [英] Escape sequences in command line arguments (Java)

查看:76
本文介绍了命令行参数中的转义序列(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据如何使用换行符

在Java中将字符串定义为"Hello \ nHello"时,它不包含"\"特点.这是换行符的转义序列:"\ n"只是一个字符.

When you define a String as "Hello\nHello" in Java, it contains no '\' character. It is an escape sequence for the line break: "\n" is just one character.

当您使用此字符串作为您的参数时程序,但是(因此字符串是在外部定义的),"\ n"是解释为两个字符:"\"和"n".

When you use this string as an argument to your program, however (so the string is defined outside), "\n" is interpreted as two characters: '\' and 'n'.

为什么也不能编译包含转义序列的命令行参数?我以为命令行参数被放置在数组String [] args中?

Why won't the command line arguments containing escape sequences be compiled as well? I thought the command line arguments are placed into the array String[] args?

并且String [] args将包含args [0] ="Hello \ nJava";

And String[] args will contain args[0] = "Hello\nJava";

推荐答案

命令行参数不是Java源代码,因此Java源代码中字符含义的规则不适用.

Command-line arguments are not Java source code, so the rules for the meaning of characters in Java source code do not apply.

对命令行参数的解释或其他内容是命令解释器的范围;Java没有被特别对待.例如,在大多数(所有?)Linux Shell中,\ n都不能用引号引起来的字符串代替:

Interpretation or otherwise of command-line arguments is the province of the command interpreter; Java is not treated specially. For example, \n is not substituted inside a quoted-string in most (all?) Linux shells:

  $ echo 'a \n b'
  a \n b

在引号之外,反斜杠-n表示字面意义上的n",与"n"相同,因为"n"在外壳上没有任何特殊之处.

Outside of quotes, backslash-n means 'literally n', which is just the same as 'n' since 'n' is not special in any way to the shell.

 $ echo a\nb
 anb

当然,Java系统可以在命令解释器之后应用自己的处理,但是大多数Linux用户会感到困惑;与其他命令相比,Java命令的行为会不一致.

Sure, the Java system could apply its own processing after the command interpreter, but most Linux users would find that confusing; Java commands would act inconsistently compared to other commands.

这篇关于命令行参数中的转义序列(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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