The String.indent()
method in Java is used to adjust the indentation of each line in a string. This method is part of the String
class and was introduced in Java 12. It provides a convenient way to add or remove spaces at the beginning of each line in a multi-line string.
Table of Contents
- Introduction
indent
Method Syntax- Examples
- Basic Usage
- Negative Indentation
- Handling Edge Cases
- Real-World Use Case
- Conclusion
Introduction
The String.indent()
method is a member of the String
class in Java. It allows you to adjust the indentation of each line in a multi-line string by adding or removing leading spaces. This method is useful for formatting text output, such as preparing text for display or logging.
indent() Method Syntax
The syntax for the indent
method is as follows:
public String indent(int n)
- n: The number of spaces to add (if positive) or remove (if negative) from the beginning of each line. If
n
is zero, the string is returned unchanged.
Examples
Basic Usage
The indent
method can be used to add spaces at the beginning of each line in a multi-line string.
Example
public class IndentExample {
public static void main(String[] args) {
String str = "Hello,\nWorld!\nWelcome to Java.";
String indentedStr = str.indent(4);
System.out.println("Original string:\n" + str);
System.out.println("Indented string:\n" + indentedStr);
}
}
Output:
Original string:
Hello,
World!
Welcome to Java.
Indented string:
Hello,
World!
Welcome to Java.
Negative Indentation
The indent
method can also be used to remove leading spaces from each line by specifying a negative value.
Example
public class NegativeIndentExample {
public static void main(String[] args) {
String str = " Hello,\n World!\n Welcome to Java.";
String indentedStr = str.indent(-4);
System.out.println("Original string:\n" + str);
System.out.println("Indented string:\n" + indentedStr);
}
}
Output:
Original string:
Hello,
World!
Welcome to Java.
Indented string:
Hello,
World!
Welcome to Java.
Handling Edge Cases
Example: Indenting an Empty String
If the string is empty, the indent
method returns an empty string.
public class IndentEmptyExample {
public static void main(String[] args) {
String str = "";
String indentedStr = str.indent(4);
System.out.println("Original string: '" + str + "'");
System.out.println("Indented string: '" + indentedStr + "'");
}
}
Output:
Original string: ''
Indented string: ''
Example: Indenting a String with No Leading Spaces
If the string contains no leading spaces and a negative value is specified, the indent
method will have no effect on those lines.
public class IndentNoLeadingSpacesExample {
public static void main(String[] args) {
String str = "Hello,\nWorld!";
String indentedStr = str.indent(-4);
System.out.println("Original string:\n" + str);
System.out.println("Indented string:\n" + indentedStr);
}
}
Output:
Original string:
Hello,
World!
Indented string:
Hello,
World!
Real-World Use Case
Example: Formatting a Code Snippet
One common use case for indent
is formatting a code snippet for display or logging purposes.
public class FormatCodeSnippetExample {
public static void main(String[] args) {
String codeSnippet = "public class HelloWorld {\n" +
" public static void main(String[] args) {\n" +
" System.out.println(\"Hello, World!\");\n" +
" }\n" +
"}";
String indentedSnippet = codeSnippet.indent(4);
System.out.println("Original code snippet:\n" + codeSnippet);
System.out.println("Indented code snippet:\n" + indentedSnippet);
}
}
Output:
Original code snippet:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Indented code snippet:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In this example, the indent
method is used to add four spaces to the beginning of each line in a code snippet, making it more readable.
Conclusion
The String.indent()
method in Java is a powerful and convenient tool for adjusting the indentation of each line in a string. It provides an easy way to format text output, making it useful for various applications such as displaying formatted code or logging. By understanding and utilizing the indent
method, you can efficiently manage text formatting tasks in your Java programs.