Page 1 of 1

About Question enthuware.ocpjp.v17.2.3675 :

Posted: Sun Jan 14, 2024 1:39 pm
by Badem48
Hi,

I'd like to point out that this question seems straightforward but actually touches on an important issue.

Consider what happens if s1 is defined like this:

Code: Select all

String s1 = """
             a \
             b \t
             c \s
             efg""";
Or would there be any difference with this variation?

Code: Select all

String s1 = """
             a \
             b \t
             c \s
             efg
             """;
In this context, I have no questions but in the explanation there's a bit of confusion as to why s1.split("\\n").length is 2 instead of 3, even though there appear to be 3 lines. Could the last element of the array be an empty string, perhaps? Maybe that can be added to the explanation.

Re: About Question enthuware.ocpjp.v17.2.3675 :

Posted: Mon Jan 15, 2024 11:23 pm
by admin
Good point. The explanation has been enhanced to include this point:
Finally, this string is being split using the new line character. String's split method returns an array of Strings. But the split method does not include trailing empty strings in the resulting array. That is why, although the string pointed to by s1 ends with a new line and thus contains 3 lines, the split method returns only 2 strings - "a b \t" and "c \s". Thus, s1.split("\\n").length returns 2.