Strings Beginner

Indent text and chain string transformations fluently.

โœ• Java 8
String[] lines = text.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
    sb.append("    ").append(line)
      .append("\n");
}
String indented = sb.toString();
โœ“ Java 12+
String indented = text.indent(4);

String result = text
    .transform(String::strip)
    .transform(s -> s.replace(" ", "-"));
See a problem with this code? Let us know.
๐Ÿ“

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()
Since JDK
12
Difficulty
Beginner
String.indent() and transform()
Available

Widely available since JDK 12 (March 2019)

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

Share ๐• ๐Ÿฆ‹ in โฌก