In Java, stripIndent() treats the string as lines, finds the smallest indentation shared by the non-blank lines, and removes that much leading white space from every line. It also removes trailing white space from each line and joins the lines with \n.
This is the same algorithm the compiler applies to a text block ("""), exposed as a method. It is useful for multi-line text that is built at runtime or read from a file, where the indentation comes from the source layout rather than the content.
stripIndent() is an instance method of java.lang.String, added in Java 15. Call it on a string, as in text.stripIndent(); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 15 or newer.
The last line counts even when it is blank. If the text ends with a line terminator, the empty last line has indentation 0, so nothing is removed. That is how a text block lets you control indentation with the position of the closing """.
Library declarations
java
public String stripIndent();
Parameters
Parameter
Description
None
stripIndent() takes no parameters.
Return Value
Return Value
Description
String
The text with incidental indentation and trailing white space removed and every line terminator normalized to \n.
Java Examples
Example 1 — Remove the Shared Indentation of a Code Snippet
Every line starts with at least four spaces; that common part is removed.
java
public class StripIndentBasic {
public static void main(String[] args) {
String code = " if (ready) {\n start();\n }";
System.out.println(code.stripIndent());
}
}
Output
if (ready) {
start();
}
How It Works
Find the minimum. The three lines are indented by 4, 8, and 4 spaces, so the shared indentation is 4.
Remove it everywhere. Four spaces are taken from each line, which moves the braces to column 0.
Relative indentation survives. The middle line had 4 extra spaces, and it still has them after the call.
Example 2 — Clean Trailing Spaces and Windows Line Endings
Messy text with \r\n, trailing spaces, and a trailing tab becomes tidy \n-separated lines.
java
public class StripIndentCleanup {
public static void main(String[] args) {
String messy = " a \r\n b\t\r\n c";
String clean = messy.stripIndent();
System.out.println(clean.replace("\n", "|"));
System.out.println(messy.length() + " -> " + clean.length());
}
}
Output
a|b|c
16 -> 5
How It Works
Two spaces of indentation. Each line starts with two spaces, so those are removed.
Trailing white space goes. The spaces after a and the tab after b are deleted, and each \r\n becomes \n.
Make the breaks visible. Replacing \n with | prints the lines on one row; 16 chars shrink to 5.
Example 3 — How the Last Line Controls the Result
Two strings that differ only after the final line break.
An empty last line has indent 0. The first string ends with \n, so the final line is empty and the minimum indentation is 0: nothing is removed.
A blank last line with spaces. In the second string the last line holds two spaces, which makes the minimum 2, so each line loses two spaces.
The blank line is emptied. Its spaces count for the minimum but are removed as trailing white space, leaving the closing bracket at the start of a line.
Frequently Asked Questions
It removes the indentation shared by all non-blank lines (and the last line), removes trailing white space from each line, and normalizes line terminators to \n.
Java 15, the release in which text blocks became a standard feature.
The compiler uses the same algorithm to remove incidental indentation from text blocks. The method lets you apply it to strings created at runtime.
Probably the string ends with a line terminator. The empty last line counts with indentation 0, so the minimum is 0.
Yes. Only the common amount is removed, so nested lines keep their extra indentation.