Skip to content

Deprecate StringUtils.replace and contains - #427

Merged
slachiewicz merged 1 commit into
masterfrom
agent/deprecate-replace-contains
Sep 10, 2026
Merged

slachiewicz merged 1 commit into
masterfrom
agent/deprecate-replace-contains

Conversation

@slachiewicz

Copy link
Copy Markdown
Member

Deprecates the StringUtils.replace and contains overloads that String has had an equivalent for since Java 5. Their only remaining value is tolerating null.

Fixes #362.

Marked:

  • replace(String, char, char) -> String.replace(char, char)
  • replace(String, String, String) -> String.replace(CharSequence, CharSequence)
  • contains(String, String) -> String.contains(CharSequence)
  • contains(String, char) -> String.indexOf(int) >= 0

Left alone: replace(..., int max) in both forms, and replaceOnce. String.replace cannot limit the number of replacements, so there is nothing to point those at.

The issue lists three methods; contains(String, char) is the fourth here. Deprecating one contains overload and not the other would read as an oversight, and its replacement is just as direct. Say the word if you would rather it stayed.

Two caveats are written into the javadoc rather than left for callers to discover, because neither replacement is a blind swap:

  • the JDK methods throw NullPointerException where these return null or false
  • "abc".replace("", "-") returns -a-b-c-, while StringUtils.replace("abc", "", "-") returns "abc" unchanged

interpolate() now calls the 4-arg replace so the class does not warn against itself. That is a delegation change only -- the 3-arg form is replace(text, repl, with, -1).

Verified: mvn -B verify on JDK 17 -> Tests run: 788, Failures: 0, Errors: 0. Spotless clean, no deprecation warnings in the build.

This change was created with AI assistance.

String has carried equivalents since Java 5, so these overloads only
exist to be null-tolerant.

Only the overloads with a direct JDK counterpart are marked. The max
variants and replaceOnce stay, because String.replace cannot limit the
number of replacements.

The javadoc spells out where the JDK method is not a drop-in swap:
"abc".replace("", "-") returns -a-b-c-, while this replace returns the
text unchanged when repl is empty. interpolate() now calls the 4-arg
overload so the class does not warn against itself.
@slachiewicz slachiewicz added deprecated Pull requests that deprecate features java Pull requests that update Java code labels Sep 10, 2026
@slachiewicz slachiewicz added this to the 3.5.0 milestone Sep 10, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review September 10, 2026 16:54
@slachiewicz
slachiewicz merged commit 079eb74 into master Sep 10, 2026
16 checks passed
@slachiewicz
slachiewicz deleted the agent/deprecate-replace-contains branch September 10, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecated Pull requests that deprecate features java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate replace and contains methods

1 participant