STRING INSTANCE METHODS

Java String indent() method

indent() changes each line's indentation. A positive number adds leading spaces, a negative number removes some leading whitespace, and the result ends with a newline.

Your first indent() call

String tekstas = "Java\nQuest";
String rezultatas = tekstas.indent(4);

System.out.print(rezultatas.replace(" ", "."));
Console output

....Java
....Quest

  1. tekstas contains two lines: Java and Quest.
  2. indent(4) adds four spaces at the start of each line.
  3. The example replaces spaces with dots using replace(" ", ".")to make the indentation visible in the console.
  4. print() is used instead of println()because indent() itself adds a newline.

Method syntax

public String indent(int n)

Parts of the indent() method
PartMeaning
Calling objectA specific String, for example, tekstas.indent(2)
Parameterint n – how many spaces to add or how much leading whitespace to remove
n > 0Adds n spaces at the start of each line
n == 0Leaves indentation unchanged but still normalizes line endings
n < 0Removes up to |n| leading whitespace characters from each line
Return typeA new String

indent() was introduced in Java 12 and is unavailable in earlier versions.

Why is indent() an instance method?

indent() is called on a specific string because it changes indentation in that String object's lines:

String rezultatas = tekstas.indent(2);

String tekstas = "Java";

String rezultatas = tekstas.indent(2);

System.out.print(rezultatas);
Console output

  Java

We therefore do not write String.indent(...). The method needs a specific String value whose lines will receive new indentation.

A positive number adds spaces

String tekstas = "pirmas\nantras\ntrecias";

String rezultatas = tekstas.indent(2);

System.out.print(rezultatas.replace(" ", "."));
Console output

..pirmas
..antras
..trecias

Given a positive number, indent() adds that many spaces to each line. Here, indent(2) adds two spaces before pirmas, antras and trecias.

indent(0) keeps indentation but adds a line ending

String tekstas = "Java";
String rezultatas = tekstas.indent(0);

System.out.print("[" + rezultatas + "]");
System.out.println(rezultatas.length());
Console output

[Java
]
5

  1. Original text Java has 4 characters.
  2. indent(0) adds no leading spaces.
  3. The result still ends with \n, so the closing bracket, ] , moves to the next line.
  4. The length becomes 5: four letters and one newline character.

A negative number removes leading whitespace

String tekstas = "....Java\n..Quest".replace(".", " ");

String rezultatas = tekstas.indent(-2);

System.out.print(rezultatas.replace(" ", "."));
Console output

..Java
Quest

The first line has four leading spaces, so indent(-2) removes two and leaves two. The second line has two leading spaces, so both are removed. A negative n never removes letters, only leading whitespace.

If less whitespace exists, only that amount is removed

String tekstas = "  Java\nQuest";

String rezultatas = tekstas.indent(-10);

System.out.print(rezultatas.replace(" ", "."));
Console output

Java
Quest

indent(-10) tries to remove up to ten leading spaces per line, but the first has only two and the second none. It therefore removes only existing leading spaces and preserves the text.

The original String stays unchanged

String tekstas = "Java";
String suTarpais = tekstas.indent(3);

System.out.println("[" + tekstas + "]");
System.out.print("[" + suTarpais + "]");
Console output

[Java]
[   Java
]

String is immutable in Java. indent() does not change the original tekstas object; it returns a new String value. Assign the result to a variable to use it later.

Empty lines are processed too

String tekstas = "Java\n\nQuest";

String rezultatas = tekstas.indent(2);

System.out.print(rezultatas.replace(" ", "."));
Console output

..Java
..
..Quest

Between Java and Quest is an empty line. indent(2) adds two spaces to it too, so the example shows a separate line with two dots.

Convenient for formatting multiline text

String zinute = """
        class Main {
            public static void main(String[] args) {
                System.out.println("Labas");
            }
        }
        """;

System.out.print(zinute.indent(2));
Start of the result

  class Main {
    public static void main(String[] args) {
      System.out.println("Labas");

indent() is useful for multiline text: code fragments, messages, generated text and text blocks. Instead of manually indenting every line, apply one method to the whole string.

The method cannot be called on null

String tekstas = null;

System.out.println(tekstas.indent(2));
Result

NullPointerException

indent() is an instance method. If a variable holds null, it refers to no String object, so Java has nothing on which to call the method and throws NullPointerException.

Common mistakes

  • Forgetting that indent() returns a new Stringrather than changing the old one.
  • Being surprised that the result ends with a newline.
  • Treating a negative number as removing letters, although it only removes leading whitespace.
  • Spaces are hard to see in the console, so examples can temporarily replace them with dots.
  • Trying the method on a Java version earlier than 12.

When should you use indent()?

Use indent()when uniformly shifting multiline text right or reducing its leading indentation. This helps format messages, code examples, text blocks and other user-facing text.