Skip to content

Fix StringUtils.substringAfter to return empty string when the separator is absent#16350

Merged
zrlw merged 2 commits into
apache:3.3from
ns3154:fix/substring-after-empty-string
Jun 24, 2026
Merged

Fix StringUtils.substringAfter to return empty string when the separator is absent#16350
zrlw merged 2 commits into
apache:3.3from
ns3154:fix/substring-after-empty-string

Conversation

@ns3154

@ns3154 ns3154 commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change?

StringUtils.substringAfter(String, int) is documented to return the empty string when the
separator is not found ("If nothing is found, the empty string is returned."), but it currently
returns the original string instead:

substringAfter("a:b", ':')  -> "b"     // ok
substringAfter("abc", ':')  -> "abc"   // expected "", actually returns "abc"

The not-found branch was copy-pasted from the neighbouring substringBefore (whose contract is
to return the original string), which left substringAfter inconsistent with both its own Javadoc
and its sibling substringAfterLast (which correctly returns the empty string). It also diverges
from commons-lang3's substringAfter.

This PR makes substringAfter return EMPTY_STRING when the separator is absent, and adds a unit
test (testSubstringAfter) covering the documented behaviour. The new test fails before the fix
(expected: <> but was: <abc>) and passes after it; the full StringUtilsTest suite stays green.

Fixes #16349

Checklist

  • Make sure there is a GitHub_issue field for the change.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Make sure gitHub actions can pass. Why the workflow is failing and how to fix it?

@codecov-commenter

codecov-commenter commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.83%. Comparing base (b75adde) to head (5a0b52b).

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     
Flag Coverage Δ
integration-tests-java21 32.16% <0.00%> (-0.01%) ⬇️
integration-tests-java8 32.23% <0.00%> (-0.02%) ⬇️
samples-tests-java21 32.11% <0.00%> (-0.06%) ⬇️
samples-tests-java8 29.83% <0.00%> (-0.01%) ⬇️
unit-tests-java11 59.05% <100.00%> (-0.02%) ⬇️
unit-tests-java17 58.56% <100.00%> (+0.01%) ⬆️
unit-tests-java21 58.54% <100.00%> (+0.02%) ⬆️
unit-tests-java25 58.53% <100.00%> (+0.01%) ⬆️
unit-tests-java8 59.08% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zrlw

zrlw commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

both substringBefore and substringAfter are not used, maybe we should delete them instead of fixing them.

@zrlw

zrlw commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@oxsean

@zrlw zrlw added the status/waiting-for-feedback Need reporters to triage label Jun 23, 2026
oxsean
oxsean previously approved these changes Jun 23, 2026
zrlw
zrlw previously approved these changes Jun 23, 2026

@zrlw zrlw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zrlw zrlw added bug and removed status/waiting-for-feedback Need reporters to triage labels Jun 23, 2026
@zrlw zrlw dismissed stale reviews from oxsean and themself via 5a0b52b June 23, 2026 09:33

@zrlw zrlw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ns3154

ns3154 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

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 StringUtils is a public class and these are public methods: removing them could silently break any downstream project that happens to use them. Fixing the documented contract felt like the safer, non-breaking option. Happy to defer to the team's preference if deletion is favoured, though!

@zrlw zrlw merged commit eb2eb29 into apache:3.3 Jun 24, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] StringUtils.substringAfter returns the original string instead of an empty string when the separator is absent

4 participants