문제

I would like know how the following scenario works.

String test = "a,b,c";

String testary[] = test.split(",");

if(!testary[0].equals("\"D")) gives?

I want to know why we are using backslash along with some char and how does the above statement works.

도움이 되었습니까?

해결책 2

It is the escape sequence. It tells interpreter to decipher it an an " instead of end to string. This might help http://docs.oracle.com/javase/tutorial/java/data/characters.html

다른 팁

When you're writing a string literal, you put it in double quotes ("). If you want to put an actual " character in the string, you have to put a backslash (\) before it so the parser knows that it's part of the string, not the ending quote. This is called "escaping" the quote character.

So the line

if(!testary[0].equals("\"D"))

tests whether the string at index 0 of the testary array does not equal the string "D (a double quote followed by the capital letter D). (The "not" part of that is the ! at the beginning.)

with the \ you are actually escaping the " that is just after the \ , so equals will test against "D

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top