Fix StringUtils.substringAfter to return empty string when the separator is absent#16350
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16350 +/- ##
============================================
Coverage 60.83% 60.83%
- Complexity 15 11754 +11739
============================================
Files 1953 1953
Lines 89208 89208
Branches 13458 13458
============================================
+ Hits 54270 54272 +2
+ Misses 29365 29362 -3
- Partials 5573 5574 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
both substringBefore and substringAfter are not used, maybe we should delete them instead of fixing them. |
|
Thanks for the suggestion, @zrlw! That's a fair point — both methods have no internal callers today. I chose to fix rather than delete because |
What is the purpose of the change?
StringUtils.substringAfter(String, int)is documented to return the empty string when theseparator is not found ("If nothing is found, the empty string is returned."), but it currently
returns the original string instead:
The not-found branch was copy-pasted from the neighbouring
substringBefore(whose contract isto return the original string), which left
substringAfterinconsistent with both its own Javadocand its sibling
substringAfterLast(which correctly returns the empty string). It also divergesfrom commons-lang3's
substringAfter.This PR makes
substringAfterreturnEMPTY_STRINGwhen the separator is absent, and adds a unittest (
testSubstringAfter) covering the documented behaviour. The new test fails before the fix(
expected: <> but was: <abc>) and passes after it; the fullStringUtilsTestsuite stays green.Fixes #16349
Checklist