Java String indent() Method

The String.indent() method in Java is used to adjust the indentation of each line in a string. 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. indent Method Syntax
  3. Examples
    • Adding Indentation
    • Removing Indentation
    • Handling Edge Cases
  4. Conclusion

Introduction

The String.indent() method is a member of the String class in Java, introduced in Java 12. It allows you to add or remove indentation from each line of a string, making it useful for formatting multiline strings.

indent Method Syntax

The syntax for the indent method is as follows:

public String indent(int n)
  • n: The number of spaces to add or remove. If n is positive, spaces are added. If n is negative, spaces are removed.

Examples

Adding Indentation

The indent method can be used to add a specified number of spaces to the beginning of each line in a string.

Example

public class IndentExample {
    public static void main(String[] args) {
        String multilineString = "Hello, World!\nWelcome to Java.\nEnjoy coding!";

        String indentedString = multilineString.indent(4);

        System.out.println("Original string:\n" + multilineString);
        System.out.println("Indented string:\n" + indentedString);
    }
}

Output:

Original string:
Hello, World!
Welcome to Java.
Enjoy coding!
Indented string:
    Hello, World!
    Welcome to Java.
    Enjoy coding!

Removing Indentation

The indent method can also be used to remove a specified number of spaces from the beginning of each line in a string.

Example

public class IndentExample {
    public static void main(String[] args) {
        String multilineString = "    Hello, World!\n    Welcome to Java.\n    Enjoy coding!";

        String unindentedString = multilineString.indent(-4);

        System.out.println("Original string:\n" + multilineString);
        System.out.println("Unindented string:\n" + unindentedString);
    }
}

Output:

Original string:
    Hello, World!
    Welcome to Java.
    Enjoy coding!
Unindented string:
Hello, World!
Welcome to Java.
Enjoy coding!

Handling Edge Cases

The indent method handles edge cases such as strings with no lines, strings with empty lines, and removing more spaces than are present.

Example

public class IndentExample {
    public static void main(String[] args) {
        String noLines = "";
        String emptyLines = "\n\n";
        String fewerSpaces = "  Hello\n  World";

        String indentedNoLines = noLines.indent(4);
        String indentedEmptyLines = emptyLines.indent(4);
        String moreUnindented = fewerSpaces.indent(-4);

        System.out.println("No lines, indented:\n'" + indentedNoLines + "'");
        System.out.println("Empty lines, indented:\n'" + indentedEmptyLines + "'");
        System.out.println("Fewer spaces, unindented:\n'" + moreUnindented + "'");
    }
}

Output:

No lines, indented:
''
Empty lines, indented:
'

'
Fewer spaces, unindented:
'Hello
World'

Conclusion

The String.indent() method in Java is used for adjusting the indentation of multiline strings. By understanding how to use this method, you can efficiently format strings in your Java applications. Whether you are adding or removing indentation or handling edge cases, the indent method provides a reliable solution for these tasks.

Comments