Skip to main content
Java 12 · Topic 2 of 2

String indent and transform

Definition​

Java 12 added String.indent (add or remove leading spaces on every line) and String.transform (pass a string through a function so you can chain steps).

Example code​

Indenting text and chaining a transformation

class Demo {
public static void main(String[] args) {
String block = "line one\nline two";
System.out.print(block.indent(4));

String result = " java "
.transform(String::strip)
.transform(s -> s + "!")
.transform(String::toUpperCase);
System.out.println(result);
}
}

// Output:
// line one
// line two
// JAVA!

Finished this page? Track your progress (saved only in your browser).