Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String index with "\n"

with this String:

String test = "hey\nyo\nsup\nyello";

and I call

System.out.println(test.indexOf("yello")); 

I get 11, how is that number produced? and does "\n" count as a character in the String?

like image 399
user3189506 Avatar asked Dec 16 '22 01:12

user3189506


1 Answers

does "\n" count as a character in the String?

Yes, in java \n is treated as a single character

h e y \n y o \n s u p \n yello
-------------------------------
0 1 2  3 4 5  6 7 8 9 10 11

Thats why you got 11 for test.indexOf("yello")


You can try this as a proof:

String str = "\n";
System.out.println(str.length());

will give you 1 as the output

like image 126
Baby Avatar answered Dec 30 '22 23:12

Baby