Java String stripIndent() Method

The String.stripIndent() method in Java is used to remove incidental leading whitespace from all lines in a string and then remove leading and trailing blank lines. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. stripIndent Method Syntax
  3. Examples
    • Removing Leading Whitespace
    • Handling Blank Lines
  4. Conclusion

Introduction

The String.stripIndent() method is a member of the String class in Java, introduced in Java 13. It allows you to remove incidental leading whitespace from all lines in a string and remove leading and trailing blank lines. This is particularly useful for formatting multiline strings, especially when working with text blocks.

stripIndent Method Syntax

The syntax for the stripIndent method is as follows:

public String stripIndent()

Examples

Removing Leading Whitespace

The stripIndent method can be used to remove incidental leading whitespace from each line in a multiline string.

Example

public class StripIndentExample {
    public static void main(String[] args) {
        String textBlock = """
                Line 1
                  Line 2
                Line 3
                """;

        String strippedText = textBlock.stripIndent();

        System.out.println("Original text block:");
        System.out.println(textBlock);
        System.out.println("Stripped text block:");
        System.out.println(strippedText);
    }
}

Output:

Original text block:
Line 1
  Line 2
Line 3

Stripped text block:
Line 1
  Line 2
Line 3

Handling Blank Lines

The stripIndent method also removes leading and trailing blank lines in addition to removing incidental leading whitespace from all lines.

Example

public class StripIndentExample {
    public static void main(String[] args) {
        String textBlock = """


                Line 1
                  Line 2
                Line 3


                """;

        String strippedText = textBlock.stripIndent();

        System.out.println("Original text block:");
        System.out.println(textBlock);
        System.out.println("Stripped text block:");
        System.out.println(strippedText);
    }
}

Output:

Original text block:



Line 1
  Line 2
Line 3



Stripped text block:
Line 1
  Line 2
Line 3

Using with Text Blocks

The stripIndent method is especially useful when working with text blocks to ensure consistent indentation.

Example

public class StripIndentExample {
    public static void main(String[] args) {
        String textBlock = """
                {
                    "name": "Alice",
                    "age": 30,
                    "city": "Wonderland"
                }
                """;

        String strippedText = textBlock.stripIndent();

        System.out.println("Original text block:");
        System.out.println(textBlock);
        System.out.println("Stripped text block:");
        System.out.println(strippedText);
    }
}

Output:

Original text block:
{
    "name": "Alice",
    "age": 30,
    "city": "Wonderland"
}

Stripped text block:
{
    "name": "Alice",
    "age": 30,
    "city": "Wonderland"
}

Conclusion

The String.stripIndent() method in Java provides a convenient way to remove incidental leading whitespace from all lines in a string and remove leading and trailing blank lines. By understanding how to use this method, you can efficiently format multiline strings in your Java applications. Whether you are working with text blocks or handling multiline strings with leading whitespace, the stripIndent method offers a reliable solution for these tasks.

Comments