Java String stripIndent() Method

The String.stripIndent() method in Java is used to remove incidental leading white space from each line in a multi-line string. This method is part of the String class and was introduced in Java 13. It provides a convenient way to clean up multi-line strings, especially those defined using text blocks.

Table of Contents

  1. Introduction
  2. stripIndent Method Syntax
  3. Examples
    • Basic Usage
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.stripIndent() method is a member of the String class in Java. It allows you to remove leading white space from each line in a multi-line string, making it easier to format and display text blocks consistently.

stripIndent() Method Syntax

The syntax for the stripIndent method is as follows:

public String stripIndent()

Examples

Basic Usage

The stripIndent method can be used to remove incidental leading white space from each line in a multi-line string.

Example

public class StripIndentExample {
    public static void main(String[] args) {
        String textBlock = """
                Line 1
                    Line 2
                        Line 3
                """;
        String strippedTextBlock = textBlock.stripIndent();
        System.out.println("Original text block:\n" + textBlock);
        System.out.println("Stripped text block:\n" + strippedTextBlock);
    }
}

Output:

Original text block:
Line 1
    Line 2
        Line 3

Stripped text block:
Line 1
    Line 2
        Line 3

Handling Edge Cases

Example: Stripping an Empty String

If the string is empty, the stripIndent method returns an empty string.

public class StripIndentEmptyExample {
    public static void main(String[] args) {
        String str = "";
        String strippedStr = str.stripIndent();
        System.out.println("Original string: '" + str + "'");
        System.out.println("Stripped string: '" + strippedStr + "'");
    }
}

Output:

Original string: ''
Stripped string: ''

Example: Stripping a String with No Leading White Space

If the string contains no leading white space, the stripIndent method returns the original string unchanged.

public class StripIndentNoLeadingWhiteSpaceExample {
    public static void main(String[] args) {
        String str = "No leading white space\nOn any line";
        String strippedStr = str.stripIndent();
        System.out.println("Original string:\n" + str);
        System.out.println("Stripped string:\n" + strippedStr);
    }
}

Output:

Original string:
No leading white space
On any line
Stripped string:
No leading white space
On any line

Real-World Use Case

Example: Formatting JSON String

One common use case for stripIndent is formatting a multi-line JSON string for display or logging purposes.

public class FormatJsonExample {
    public static void main(String[] args) {
        String json = """
                {
                    "name": "Alice",
                    "age": 30,
                    "city": "Wonderland"
                }
                """;
        String strippedJson = json.stripIndent();
        System.out.println("Original JSON string:\n" + json);
        System.out.println("Stripped JSON string:\n" + strippedJson);
    }
}

Output:

Original JSON string:
{
    "name": "Alice",
    "age": 30,
    "city": "Wonderland"
}

Stripped JSON string:
{
"name": "Alice",
"age": 30,
"city": "Wonderland"
}

In this example, the stripIndent method is used to remove incidental leading white space from the JSON string, making it easier to read.

Conclusion

The String.stripIndent() method in Java is used for cleaning up multi-line strings by removing incidental leading white space. It is particularly useful when working with text blocks, making your code more readable and maintainable. By understanding and utilizing the stripIndent method, you can efficiently manage text formatting tasks in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top