String.indent() and transform()
Indent text and chain string transformations fluently.
Porównanie kodu
✕ 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(" ", "-"));
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
Manual Indentation
Nowoczesne podejście
indent() / transform()
Od JDK
12
Poziom trudności
Początkujący
Wsparcie JDK
String.indent() and transform()
Dostępne
Widely available since JDK 12 (March 2019)
Jak to działa
indent(n) adds n spaces to each line. transform(fn) applies any function and returns the result, enabling fluent chaining of string operations.
Powiązana dokumentacja