System.out.println(",".split(",", 0).length);
System.out.println("".split(",", 0).length);
prints:
0
1
This seems odd. According to the documentation for String.split(pattern, n),
If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
In the second case, when splitting an empty string, this rule seems to be ignored. Is this expected behavior?
As from docs
If the expression does not match any part of the input then the
resulting array has just one element, namely this string
"".split(",", 0).length mean it is similar to this
System.out.println(new String[]{""}.length);
There was no , in the string "" so the array contain single element "" an empty string , result in array length as 1
another example
System.out.println("aaa".split(",", 0).length); // 1
System.out.println("aaa".split("," , 0)[0]); // aaa
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With