GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev โ String.indent() and transform() | java.evolved
Strings
Beginner
String.indent() and transform()Indent text and chain string transformations fluently. 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(" ", "-"));
See a problem with this code? Let us know.
Why the modern way wins
๐
Built-inIndentation is a common operation โ now it's one call. ๐
Chainabletransform() enables fluent pipelines on strings. ๐งน
Clean codeNo 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
Proof
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
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();
JDK 11+
โ
Strings
Beginner
String.lines() for line splittingJava 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);
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. |