โ— Shell
clean mode source โ†—

String.indent() and transform()

Home / Strings / String.indent() and transform()

Code Comparison

String[] lines = text.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
    sb.append("    ").append(line)
      .append("\n");
}
String indented = sb.toString();
String indented = text.indent(4);

String result = text
    .transform(String::strip)
    .transform(s -> s.replace(" ", "-"));

Why the modern way wins

๐Ÿ“

Built-in

Indentation is a common operation โ€” now it's one call.

๐Ÿ”—

Chainable

transform() enables fluent pipelines on strings.

๐Ÿงน

Clean code

No manual line splitting and StringBuilder loops.

Old Approach

Manual Indentation

Modern Approach

indent() / transform()

JDK Support

String.indent() and transform()

Available

Widely available since JDK 12 (March 2019)

How it works

indent(n) adds n spaces to each line. transform(fn) applies any function and returns the result, enabling fluent chaining of string operations.

Related Documentation

Proof