java 反斜杠 (\) 表现不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11493006/
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
Backslash (\) behaving differently
提问by Fahim Parkar
I have small code as shown below
我有如下所示的小代码
public class Testing {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String firstString = sc.next();
System.out.println("First String : " + firstString);
String secondString = "text\";
System.out.println("Second String : " + secondString);
}
}
When I provide input as text\\I get output as
当我提供输入作为文本\\我得到输出
First String : text\
Second String : text\
Why I am getting two different string when input I provide to first string is same as second string.
为什么当我提供给第一个字符串的输入与第二个字符串相同时,我得到两个不同的字符串。
Demo at www.ideone.com
在 www.ideone.com 上进行演示
回答by Martijn Courteaux
The double backslash in the console you provide as input on runtime are really two backslashes. You simply wrote two times ASCII character backslash.
您在运行时提供的控制台中的双反斜杠实际上是两个反斜杠。你只是写了两次 ASCII 字符反斜杠。
The double backslash inside the string literal means only one backslash. Because you can't write a single backslash in the a string literal. Why? Because backslash is a special character that is used to "escape" special characters. Eg: tab, newline, backslash, double quote. As you see, backslash is also one of the character that needs to be escaped. How do you escape? With a backslash. So, escaping a backslash is done by putting it behind a backslash. So this results in two backslashes. This will be compiled into a single backslash.
字符串文字中的双反斜杠表示只有一个反斜杠。因为你不能在字符串文字中写一个反斜杠。为什么?因为反斜杠是一个特殊字符,用于“转义”特殊字符。例如:制表符、换行符、反斜杠、双引号。如您所见,反斜杠也是需要转义的字符之一。你怎么逃?带反斜杠。因此,转义反斜杠是通过将其放在反斜杠后面来完成的。所以这会导致两个反斜杠。这将被编译成一个反斜杠。
Why do you have to escape characters? Look at this string: this "is" a string
. If you want to write this as a string literal in Java, you might intentionally think that it would look like this:
为什么要转义字符?看看这个字符串:this "is" a string
. 如果你想在 Java 中把它写成一个字符串文字,你可能会故意认为它看起来像这样:
String str = "this "is" a string";
As you can see, this won't compile. So escape them like this:
如您所见,这不会编译。所以像这样逃避他们:
String str = "this \"is\" a string";
Right now, the compiler knows that the "
doesn't close the string but really means character "
, because you escaped it with a backslash.
现在,编译器知道 the"
不会关闭字符串,但实际上意味着 character "
,因为您使用反斜杠对其进行了转义。
回答by Pshemo
In Strings \
is special character, for example you can use it like \n
to create new line sign. To turn off its special meaning you need to use another \
like \\
. So in your 2nd case \\
will be interpreted as one \
character.
在字符串中\
是特殊字符,例如您可以使用它\n
来创建新的行号。要关闭它的特殊含义,您需要使用另一个\
like \\
。所以在你的第二种情况下\\
将被解释为一个\
字符。
In case when you are reading Strings from outside sources (like streams) Java assume that they are normal characters, because special characters had already been converted to for example tabulators, new line chars, and so on.
如果您从外部源(如流)读取字符串,Java 会假定它们是普通字符,因为特殊字符已经被转换为例如制表符、换行符等。
回答by cl-r
Java use the \
as an escape character in the second string
Java 使用 the\
作为第二个字符串中的转义字符
EDITED on demand
按需编辑
In the first case, the input take all the typed characters and encapsulate them in a String, so all characters are printed (no evaluation, as they are read, they are printed).
In the second, JVM evaluate the String between
"
, character by character, and the first\
is read has a meta character protecting the second one, so it will not be printed.
在第一种情况下,输入获取所有键入的字符并将它们封装在一个字符串中,因此所有字符都被打印出来(没有评估,因为它们被读取,它们被打印出来)。
在第二个中,JVM
"
逐个字符地评估 之间的字符串,第一个\
被读取具有保护第二个的元字符,因此它不会被打印。
回答by Nahuel Fouilleul
String internally sequence of char must not be confused with the sequence of char between double quotes specially because backslash has a special meaning: "\n\r\t\\\0" => { (char)10,(char)13,(char)9,'\\',(char)0 }
字符串内部的char序列一定不要与双引号之间的char序列混淆,因为反斜杠有特殊的含义:"\n\r\t\\\0" => { (char)10,(char)13, (char)9,'\\',(char)0 }