Java String indent() Method

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

Definition

In Java, indent(n) splits the string into lines, adjusts the start of each line, and joins them again with \n, adding a \n after the last line too. A positive n inserts n spaces; a negative n removes up to |n| leading white space chars.

It is handy for nesting text in reports, quoting messages, or printing code at a deeper level. Calling indent(0) changes no indentation but still normalizes every line terminator to \n.

indent() is an instance method of java.lang.String, added in Java 12. Call it on a string, as in text.indent(...); 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 12 or newer.

Syntax

Pass the number of spaces to add (positive) or remove (negative).

java
String nested  = block.indent(4);    // 4 spaces in front of each line
String outdent = block.indent(-2);   // up to 2 white space chars removed
CallWhat it does
"a\nb".indent(2)" a\n b\n".
" a".indent(-2)" a\n".
"a\r\nb".indent(0)"a\nb\n".

Because a final \n is always added, print the result with System.out.print rather than println to avoid an extra blank line.

Library declarations
java
public String indent(int n);

Parameters

ParameterDescription
nSpaces to add (positive), white space chars to remove (negative), or 0 to only normalize line endings.

Return Value

Return ValueDescription
StringThe adjusted lines, each ending with \n; "" for an empty string.

Java Examples

Example 1 — Indent a Menu by Four Spaces

Every line gets the same prefix, and a newline is added at the end.

java
public class IndentAdd {
    public static void main(String[] args) {
        String menu = "Start\nOptions\nQuit";

        System.out.print(menu.indent(4));
        System.out.println("[end]");
    }
}

How It Works

  1. Line by line. indent(4) inserts four spaces at the start of each of the three lines.
  2. A trailing newline appears. The result ends with \n even though menu did not.
  3. Print, not println. Because of that final newline, print is enough, and [end] lands on its own line.

Example 2 — Remove Up to Four Leading Spaces

Lines with less indentation lose what they have; dots make the remaining spaces visible.

java
public class IndentRemove {
    public static void main(String[] args) {
        String text = "      deep\n  shallow\nnone";
        String result = text.indent(-4);

        System.out.print(result.replace(' ', '.'));
    }
}

How It Works

  1. Six minus four. The first line had 6 spaces; removing 4 leaves 2, shown as two dots.
  2. Never more than exists. " shallow" has only 2 spaces, so both are removed and nothing else.
  3. Unindented lines stay. "none" has no leading white space, so it is copied as is, followed by \n.

Example 3 — Normalize Line Endings with indent(0)

Mixed Windows and old Mac line endings become plain \n.

java
public class IndentZero {
    public static void main(String[] args) {
        String mixed = "one\r\ntwo\rthree";
        String normalized = mixed.indent(0);

        System.out.println(normalized.replace("\n", "|"));
        System.out.println("ends with \\n: " + normalized.endsWith("\n"));
        System.out.println("contains \\r: " + normalized.contains("\r"));
    }
}

How It Works

  1. Three kinds of break. The input uses \r\n after one and a lone \r after two.
  2. No spaces change. With n = 0 the lines keep their content, but each now ends with \n, shown as |.
  3. No carriage returns left. contains("\r") is false, and the text ends with a newline.

Frequently Asked Questions

It adds n spaces to the start of every line (or removes up to n white space chars for negative n) and ends every line with \n.
Java 12.
It normalizes every line, including the last, to end with \n. Use print instead of println, or strip the final \n if you do not want it.
It removes up to that many leading white space chars from each line; lines with fewer lose all their leading white space.
Call text.indent(0). It keeps the content and indentation but converts \r\n and \r to \n.

Next: indexOf()

Find the first position of a char or substring (-1 if absent).

indexOf() 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.

10 people found this page helpful