Question

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.

Était-ce utile?

La solution 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

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top