Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

In a code review you find a StringUtils class in your service with 40 hand-written helpers (isNullOrEmpty, padLeft, capitalize, join, reverse, countMatches), and three other libraries offering the same. What do you recommend, given what the JDK String API now provides?

Most of those 40 helpers now exist in the JDK itself, through methods such as isBlank, strip, repeat, lines, indent, formatted, String.join, codePoints, transform and, since Java 21, splitWithDelimiters, so I would delete the duplicates, keep at most one external library such as Apache Commons Lang for the genuine extras, and check every helper for locale, Unicode and null-handling behaviour before replacing it. The replacement happens in small, tested steps, with OpenRewrite or IDE refactorings for the mechanical parts, and a static analysis or ArchUnit rule stops new copies appearing.

Why this question is asked

Almost every long-lived Java service has a home-made StringUtils. It was usually written for good reasons in the Java 6 or 8 days, when the String API was thin, and then grew as each developer added the helper they needed. When three libraries offering the same functions are also on the classpath, nobody knows which one is correct, and subtle differences between them become bugs. Interviewers use this scenario to see how we balance cleanup against risk. In this answer, we will see what the JDK now provides, decide what to keep, look at the hidden bugs that live in hand-written helpers, and plan a safe migration and a rule that stops the problem coming back.

1What the JDK String API Covers Today

The String class gained a lot between Java 11 and Java 21. Java 11 added isBlank(), strip(), stripLeading(), stripTrailing(), repeat() and lines(). Java 12 added indent() and transform(). Java 15 added stripIndent(), translateEscapes() and formatted(). Java 21 added splitWithDelimiters() and indexOf with a begin and end range, and StringBuilder gained repeat(). Together with older methods such as String.join(), chars() and codePoints(), this covers most helpers in a typical StringUtils.

Java
" ".isBlank(); // true
"ab".repeat(3); // ababab
String.join(", ", List.of("x", "y")); // x, y
"%5s|".formatted("ab"); // " ab|" padLeft to width 5
"a,b;c".splitWithDelimiters("[,;]", 0); // [a, ",", b, ";", c] Java 21
"x\ny".indent(2); // " x\n y\n"
new StringBuilder("abc").reverse().toString(); // cba, keeps surrogate pairs together
Mapping common helpers
Helper in our StringUtilsReplacementNotes
isNullOrEmpty / isNullOrBlanks == null || s.isBlank()Or Commons Lang StringUtils.isBlank
padLeft / padRight"%5s".formatted(s), repeat()Width is in chars, not code points
joinString.join, Collectors.joiningJDK since Java 8
reversenew StringBuilder(s).reverse()Surrogate-safe, unlike most hand loops
capitalizeKeep in one libraryNeeds a Locale decision
countMatchesKeep in one library or a small loopNo direct JDK method
The JDK covers most of the list; a short remainder justifies at most one library.

Notice that not everything has a direct JDK equivalent. There is no null-safe isNullOrEmpty, no capitalize and no countMatches. Those are either one-liners we can write inline or reasons to keep one library.

2Keep at Most One Library

Three libraries that do the same thing are worse than one, because each has slightly different contracts. Apache Commons Lang's StringUtils is null-safe almost everywhere: StringUtils.isBlank(null) is true and StringUtils.capitalize(null) returns null. Spring's own StringUtils exists mainly for the framework's internal use and has a different set of methods. Guava's Strings offers a few more. A developer who mixes them gets different null behaviour on every line.

So I would choose one library, usually Commons Lang because most teams already know it, for the helpers the JDK does not have, and remove the others from the dependency list. Our own StringUtils should shrink to the few helpers that are truly specific to our domain, such as masking an account number, and it should then probably get a more specific name such as AccountNumberFormat.

3Check Each Helper Before Replacing It

Hand-written helpers often contain bugs that the rest of the code now depends on, so replacing them blindly can change behaviour. We look for three kinds. Locale bugs: a helper calling toUpperCase() without a Locale behaves differently on a server with a Turkish default locale. Unicode bugs: a helper that reverses or truncates by char index splits emoji and other characters made of surrogate pairs. Null contracts: a helper that returns an empty string for null will cause a NullPointerException when replaced with a JDK method.

Java
static String reverse(String s) { // our old helper
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) sb.insert(0, c);
return sb.toString();
}
reverse("a\uD83D\uDE00"); // the emoji's two halves come out swapped: broken text
new StringBuilder("a\uD83D\uDE00").reverse(); // the emoji, then a: correct
static boolean isNullOrEmpty(String s) { return s == null || s.trim().isEmpty(); }
isNullOrEmpty("\u2003"); // false: trim() ignores the em space
"\u2003".isBlank(); // true: behaviour changes if we switch

So before each replacement, we write characterization tests: tests that record what the helper does today for null, empty, blank, Unicode and very long input. Then we decide for each difference whether the old behaviour was a bug to fix or a contract to keep.

4Replace in Small, Tested Steps

We never replace 40 helpers in one pull request. We mark the old helpers @Deprecated(forRemoval = true) with a Javadoc pointer to the replacement, so every use shows a warning. Then we move one helper, or one group of similar helpers, at a time, each change small enough to review properly. For mechanical rewrites, IDE inline refactorings and OpenRewrite recipes can change hundreds of call sites consistently; OpenRewrite has recipes for common Commons Lang to JDK migrations (check the recipe catalogue for current names). When a helper has no callers left, we delete it.

Step throughRetiring one helper
  1. Write characterization testsnull, empty, blank, Unicode, long input
  2. Same behaviour as the replacement?
    YesRewrite callers with IDE or OpenRewriteNoDecide: fix the bug or keep a wrapper
  3. Deprecate the helper@Deprecated(forRemoval = true)
  4. Any callers left?
    YesRepeat next sprintNoDelete the helper

4 steps in order. Play them, or step through them one at a time.

5Stop New Copies From Appearing

Cleanup that is not protected will be undone within a year. We add rules that run in every build. An ArchUnit test can fail when a class named StringUtils appears outside an allowed package, or when code depends on the libraries we removed. A Maven Enforcer bannedDependencies rule, or the Gradle equivalent, keeps the duplicate libraries off the classpath. Static analysis tools can flag toUpperCase() and toLowerCase() without a Locale. Finally, a short entry in the team's coding guide says which library is the chosen one and why, so new developers do not have to guess.

6Follow-up questions

What does String.transform do and when is it useful?

transform(Function<? super String, ? extends R> f), added in Java 12, simply applies the function to the string and returns the result, so s.transform(f) is the same as f.apply(s). Its value is readability: it lets us insert our own function into a fluent chain of String calls without breaking the chain. For example, input.strip().transform(Slugs::toSlug).toLowerCase(Locale.ROOT) reads top to bottom, where the alternative nests calls inside each other. It can also return a different type, such as raw.transform(Integer::parseInt).

Why not just leave the 40 helpers in place if they work?

Because they are code we own, test and must keep correct, and they often are not correct in edge cases such as Unicode or locale. Duplicates also cause inconsistency: two helpers named capitalize in different libraries may behave differently with null. That said, the cleanup must earn its cost, so I would start with the helpers that have known bugs or many duplicates and leave stable, domain-specific helpers alone.

Is it a problem that the JDK methods are not null-safe?

It is a difference, not a problem. JDK String methods are instance methods, so calling one on null throws NullPointerException, while Commons Lang treats null as a value. In modern code we prefer to keep nulls out at the boundary, for example with validation on request objects, so most inner code never sees a null string. Where nulls are legitimate, we either check explicitly or keep the null-safe library method.

7Conclusion

In this answer, we saw that the JDK now covers most string helpers, from isBlank, strip and repeat in Java 11 to formatted, transform and splitWithDelimiters, so a 40-method StringUtils and three overlapping libraries can shrink to one chosen library plus a few domain helpers. Before replacing anything, we check for locale, Unicode and null-handling differences with characterization tests, then replace in small steps using deprecation, IDE refactorings and OpenRewrite, and protect the result with ArchUnit, banned-dependency and static analysis rules.

What to say in the interviewAbout 90 seconds aloud

Open with

I would recommend shrinking it, but carefully. The JDK now covers most of these helpers: isBlank, strip, repeat and lines came in Java 11, indent and transform in 12, stripIndent and formatted in 15, and splitWithDelimiters in 21, plus String.join, Collectors.joining, codePoints and StringBuilder.reverse, which handles surrogate pairs correctly.

Then, if they want more

For what is left, like capitalize, countMatches or null-safe checks, I would keep one library, usually Commons Lang because people know it, and remove the other two so we do not mix three null contracts.

Before replacing anything I would check each helper for hidden behaviour: toUpperCase without a Locale, which breaks on a Turkish server; reverse or truncate by char, which splits emoji; trim-based blank checks that miss Unicode spaces; and what each one does with null.

I would write characterization tests for null, empty, blank, Unicode and long input, and decide case by case whether a difference is a bug to fix or a contract to keep.

Then I would deprecate the old helpers for removal and migrate one group at a time, using IDE refactorings or OpenRewrite recipes for the mechanical changes, deleting each helper when its callers are gone.

Finally I would add an ArchUnit rule against new StringUtils classes, a banned-dependencies rule for the removed libraries, and a static analysis rule for case conversion without a locale, so the problem does not come back.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Java 11, Java 12, Java 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In a code review you find a StringUtils class in your service with 40 hand-written...": https://scrivio-interview-library.netlify.app/java/strings/68ed9d17dd/