Java String stripIndent() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Java 15+

Definition

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.

Syntax

Call it with no arguments on multi-line text.

java
String code  = "    if (ok) {\n        run();\n    }";
String clean = code.stripIndent();   // "if (ok) {\n    run();\n}"
CallWhat it does
" a\n b".stripIndent()"a\nb".
" a\n b".stripIndent()"a\n b": relative indent stays.
"a \r\nb".stripIndent()"a\nb": trailing spaces and \r\n cleaned.

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

ParameterDescription
NonestripIndent() takes no parameters.

Return Value

Return ValueDescription
StringThe 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());
    }
}

How It Works

  1. Find the minimum. The three lines are indented by 4, 8, and 4 spaces, so the shared indentation is 4.
  2. Remove it everywhere. Four spaces are taken from each line, which moves the braces to column 0.
  3. 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());
    }
}

How It Works

  1. Two spaces of indentation. Each line starts with two spaces, so those are removed.
  2. Trailing white space goes. The spaces after a and the tab after b are deleted, and each \r\n becomes \n.
  3. 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.

java
public class StripIndentLastLine {
    public static void main(String[] args) {
        String endsWithNewline = "    x\n      y\n";
        String endsWithSpaces = "    x\n      y\n  ";

        System.out.println("[" + endsWithNewline.stripIndent() + "]");
        System.out.println("[" + endsWithSpaces.stripIndent() + "]");
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: stripLeading()

Remove Unicode whitespace from the start only (Java 11+).

stripLeading() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful