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 negativen 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
Call
What 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
Parameter
Description
n
Spaces to add (positive), white space chars to remove (negative), or 0 to only normalize line endings.
Return Value
Return Value
Description
String
The 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]");
}
}
Output
Start
Options
Quit
[end]
How It Works
Line by line.indent(4) inserts four spaces at the start of each of the three lines.
A trailing newline appears. The result ends with \n even though menu did not.
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(' ', '.'));
}
}
Output
..deep
shallow
none
How It Works
Six minus four. The first line had 6 spaces; removing 4 leaves 2, shown as two dots.
Never more than exists." shallow" has only 2 spaces, so both are removed and nothing else.
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"));
}
}
Output
one|two|three|
ends with \n: true
contains \r: false
How It Works
Three kinds of break. The input uses \r\n after one and a lone \r after two.
No spaces change. With n = 0 the lines keep their content, but each now ends with \n, shown as |.
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.