Empty strings will be omitted from the end no matter how many times you repeat the pattern.
Originally created by @sin-ack on GitHub (Aug 18, 2025).
Reproduced in 0.29.0.
Reproducer:
```pkl
// 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.
@StefMa commented on GitHub (Aug 18, 2025):
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.
@bioball commented on GitHub (Aug 18, 2025):
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:
```pkl
"abcababab".splitLimit("ab", import("pkl:math").maxInt)
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @sin-ack on GitHub (Aug 18, 2025).
Reproduced in 0.29.0.
Reproducer:
Empty strings will be omitted from the end no matter how many times you repeat the pattern.
@StefMa commented on GitHub (Aug 18, 2025):
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.
@bioball commented on GitHub (Aug 18, 2025):
I agree that the behavior seems incorrect. However, fixing this now would be a breaking change.
To get your expected behavior, you can use
splitLimitinstead. For example:@sin-ack commented on GitHub (Aug 18, 2025):
Thanks for the workaround!