Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: What is String.stripIndent() in Java 15?
Answer:

Suppose we have a multi-line string where each line is intended and has some trailing spaces, such as the following. We print each line, bounded by two vertical bars.

String html = """
<html> s
<body> s
<h1>Hello!</h1>
</body> s
</html> s
""";

html.lines()
.map(line -> "|" + line + "|")
.forEachOrdered(System.out::println);
Code language: Java (java)

As you learned in the first chapter, the alignment of a text block is based on the closing quotation marks. The output, therefore, looks like this:

|  <html>     |
| <body> |
| <h1>Hello!</h1>|
| </body> |
| </html> |

Using the stripIndent() method, we can remove the indentation and trailing spaces:

html.stripIndent()
.lines()
.map(line -> "|" + line + "|")
.forEachOrdered(System.out::println);

The output is now:

|<html>|
| <body>|
| <h1>Hello!</h1>|
| </body>|
|</html>|
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook