Java String indent()

Introduction

The String.indent() method, introduced in Java 12, provides a simple way to adjust the indentation of each line in a multi-line string. This method allows you to add or remove spaces (indentation) from each line, making it particularly useful for formatting text, such as code or structured content.

  • Positive argument: Adds the specified number of spaces at the start of each line.
  • Negative argument: Removes spaces but only up to the number of leading spaces present.
  • Handles empty lines: Maintains empty lines, adjusting spaces while preserving line breaks.

Program Steps

  1. Indent a Multi-Line String: Use a positive number with indent() to add spaces at the start of each line.
  2. Remove Indentation: Use a negative number with indent() to remove leading spaces.
  3. Handle Empty Lines: Use indent() to adjust spaces for both non-empty and empty lines.
  4. Practical Use: Combine indent() with other string operations for formatting structured content, such as code.

Java Programs

Example 1: Indent a Multi-Line String

public class StringIndentExample {
    public static void main(String[] args) {
        // Step 1: Create a multi-line string
        String text = "Java is fun.\nJava is powerful.\nJava 12 introduced new features.";

        // Step 2: Add indentation to each line
        String indentedText = text.indent(4);

        // Step 3: Display the indented string
        System.out.println("Indented Text:\n" + indentedText);
    }
}

Output

Indented Text:
    Java is fun.
    Java is powerful.
    Java 12 introduced new features.

Explanation

  • Step 1: Create a multi-line string using \n to separate lines.

    String text = "Java is fun.\nJava is powerful.\nJava 12 introduced new features.";
    
  • Step 2: Add indentation by calling indent(4), which adds four spaces at the start of each line.

    String indentedText = text.indent(4);
    
  • Step 3: Print the result, displaying each line with four spaces at the beginning.

Example 2: Remove Indentation from a String

Java Program

public class StringRemoveIndentExample {
    public static void main(String[] args) {
        // Step 1: Create a multi-line string with indentation
        String text = "    Line 1\n    Line 2\n    Line 3";

        // Step 2: Remove indentation from each line
        String unindentedText = text.indent(-4);

        // Step 3: Display the unindented string
        System.out.println("Unindented Text:\n" + unindentedText);
    }
}

Output

Unindented Text:
Line 1
Line 2
Line 3

Explanation

  • Step 1: Create a multi-line string that is already indented by four spaces.

    String text = "    Line 1\n    Line 2\n    Line 3";
    
  • Step 2: Use indent(-4) to remove four leading spaces from each line.

    String unindentedText = text.indent(-4);
    
  • Step 3: Print the result, showing the lines without leading spaces.

Example 3: Handling Empty Lines with indent()

Java Program

public class StringIndentWithEmptyLinesExample {
    public static void main(String[] args) {
        // Step 1: Create a multi-line string with empty lines
        String text = "Line 1\n\nLine 2\n\nLine 3";

        // Step 2: Add indentation to each line
        String indentedText = text.indent(3);

        // Step 3: Display the indented string
        System.out.println("Indented Text with Empty Lines:\n" + indentedText);
    }
}

Output

Indented Text with Empty Lines:
   Line 1

   Line 2

   Line 3

Explanation

  • Step 1: Create a multi-line string with some empty lines.

    String text = "Line 1\n\nLine 2\n\nLine 3";
    
  • Step 2: Use indent(3) to add three spaces to each line, including the empty lines.

    String indentedText = text.indent(3);
    
  • Step 3: Print the indented text. The empty lines are preserved with added spaces.

Example 4: Formatting Code using indent()

Java Program

public class StringIndentForCodeExample {
    public static void main(String[] args) {
        // Step 1: Create a string representing code
        String code = "public class Example {\n" +
                      "    public static void main(String[] args) {\n" +
                      "        System.out.println(\"Hello, World!\");\n" +
                      "    }\n" +
                      "}";

        // Step 2: Indent the entire block of code by 4 spaces
        String indentedCode = code.indent(4);

        // Step 3: Display the indented code
        System.out.println("Indented Code:\n" + indentedCode);
    }
}

Output

Indented Code:
    public class Example {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }

Explanation

  • Step 1: Create a string that represents Java code.

    String code = "public class Example {\n" +
                  "    public static void main(String[] args) {\n" +
                  "        System.out.println(\"Hello, World!\");\n" +
                  "    }\n" +
                  "}";
    
  • Step 2: Use indent(4) to add four spaces at the start of each line.

    String indentedCode = code.indent(4);
    
  • Step 3: Print the result, showing the entire block of code indented further.

Conclusion

The String.indent() method simplifies formatting by adding or removing spaces from each line of a string. Whether adjusting indentation for readability, formatting source code, or dealing with empty lines, this method provides a flexible and efficient solution for manipulating multi-line strings.

Comments