What happens is that the string looks like:
"This is the first line\nAnd this is the second"
So when you split it by " "
you get:
"line\nAnd"
When you print it, it looks like two separate strings.To demonstrate this, try adding an extra print in the for
loop:
for (final String s : string.split(" ")) {
if (s.contains("\n")) {
System.out.print(s);
System.out.println(" END");
}
}
Output:
line
And END
And when you try to check whether a string starts or ends with "\n"
you won't get any result because in fact the string "line\nAnd"
doesn't start or end with "\n"