`String.split` omits final empty string(s) for trailing pattern
Reproduced in 0.29.0.
Reproducer:
// Expected: List("", "c", "", "")
// Got: List("", "c")
"abcababab".split("ab")
// Works as expected: List("", "1")
".1".split(".")
// Expected: List("", "1", "")
// Got: List("", "1")
".1.".split(".")
Empty strings will be omitted from the end no matter how many times you repeat the pattern.
The issue occurs because the implementation of String.split in the codebase ultimately delegates to Java’s String.split method. By default, Java’s split omits trailing empty strings from the result. This is reflected in StringNodes.java, where the split method calls self.split(Pattern.quote(separator)), so when the separator appears at the end of the string or multiple times in succession, the resulting list omits empty strings at the end—matching the behavior you observed in your examples.
I agree that the behavior seems incorrect. However, fixing this now would be a breaking change.
To get your expected behavior, you can use splitLimit instead. For example:
"abcababab".splitLimit("ab", import("pkl:math").maxInt)
Thanks for the workaround!