Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java String.lines() and stripIndent() methods

Reference for Java String.lines() (Java 11), indent() (12), stripIndent() and translateEscapes() (15): split text into lines and tidy indentation.

lines() returns a Stream<String> of the lines in a string. A line ends at \n, \r or \r\n, so it handles text from Windows, Unix and old Mac sources alike, and the line terminators are not included in the elements. It was added in Java 11.

A few companion methods deal with indentation and escapes: indent(n) (Java 12) shifts every line, stripIndent() (Java 15) removes the common leading whitespace the way a text block does, and translateEscapes() (Java 15) processes backslash escapes in a string. They are most useful together with multi-line text blocks.

Java
long n = "a\nb\r\nc".lines().count(); // 3

Try it

Java Working with lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java indent(), stripIndent() and translateEscapes()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public Stream<String> lines()           // since 11
public String indent(int n)             // since 12
public String stripIndent()             // since 15
public String translateEscapes()        // since 15
Name Description
n Spaces to add (positive) or remove (negative) at the start of every line.

Returns: lines() a stream of lines without terminators; the others a new String. indent() normalizes every line break to \n and appends one if the result does not end with it.

Throws: translateEscapes() throws IllegalArgumentException for an invalid escape sequence. The others do not throw.

Usage notes

  • Terminators are recognized, not kept. If you need to keep them, use split("(?<=\n)") or a Scanner.
  • Trailing blank handling: a final line terminator does not add an element, but a blank line before it does. An empty string has zero lines, and "\n" has one empty line.
  • The stream is lazy, so lines().findFirst() on a huge string does not scan all of it.
  • indent() and blank lines. Blank lines are indented too when n > 0; with a negative n, only existing leading whitespace is removed, never more than n characters.
  • stripIndent() is what the compiler does to text blocks. It also normalizes line endings to \n and strips trailing spaces from each line. The last line counts for the common indentation even if it holds only whitespace.
  • translateEscapes() accepts the Java literal escapes (\n, \t, \", octal, \s and so on) but not \uXXXX sequences.
  • Reading files: Files.lines(path) and BufferedReader.lines() give the same kind of stream from a file, and must be closed.