GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • โ†
  • โ†’

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev โ€” String.indent() and transform() | java.evolved

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

String.indent() and transform()

  • ๐Ÿ› Report a code issue
  • ๐ŸŒ Report a translation issue
  • ๐Ÿ’ก Suggest a new pattern

Indent text and chain string transformations fluently.

Strings Formatting
Code Comparison
โœ• 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(" ", "-"));
Share ๐•
See a problem with this code? Let us know.
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()
Since JDK
12
Difficulty
Beginner
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
String.indent() โ†— String.transform() โ†—
Proof
View proof source โ†—
Related patterns
Strings Beginner

String.isBlank()

Java 8
boolean blank =
    str.trim().isEmpty();
// or: str.trim().length() == 0
Java 11+
boolean blank = str.isBlank();
// handles Unicode whitespace too
Hover to see modern โžœ
JDK 11+ โ†’
Strings Beginner

String.strip() vs trim()

Java 8
// trim() only removes ASCII whitespace
// (chars <= U+0020)
String clean = str.trim();
Java 11+
// strip() removes all Unicode whitespace
String clean = str.strip();
String left  = str.stripLeading();
String right = str.stripTrailing();
Hover to see modern โžœ
JDK 11+ โ†’
Strings Beginner

String.lines() for line splitting

Java 8
String text = "one\ntwo\nthree";
String[] lines = text.split("\n");
for (String line : lines) {
    System.out.println(line);
}
Java 11+
String text = "one\ntwo\nthree";
text.lines().forEach(IO::println);
Hover to see modern โžœ
JDK 11+ โ†’
You are visitor 01402503 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.