Java String Indent() Method

Java String Indent() Method | This blog explains the indent() method available in the Java String class. The indent() method is introduced in the JDK 12 in the java.lang.String class. It is helpful to adjust the indentation of the string or to add or remove white spaces to the string.

The syntax of the String indent() method is as follows:-
public String indent(int n)
Parameter:- takes integer parameter n as input and does the indentation.
Return type:- string
Returns:- string after indentation.

The indent() method adjusts the indentation of each line of this string based on the value of “n”, and normalizes line termination characters. This string is conceptually separated into lines using String.lines(). Each line is then adjusted as described below and then suffixed with a line feed “\n” (U+000A). The resulting lines are then concatenated and returned.

1) If n > 0 then “n” spaces (U+0020) are inserted at the beginning of each line.

2) If n < 0 then up to “n” white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed. Each white-space character is treated as a single character. In particular, the tab character “\t” (U+0009) is considered a single character; it is not expanded.

3) If n == 0 then the line remains unchanged. However, line terminators are still normalized.

N > 0N spaces are inserted at the beginning of each line.
N < 0N white space characters are removed from the beginning of each line.
N==0The line remains unchanged, but line terminators are still normalized.

Java String Indent() Method Example

public class Main {
   public static void main(String args[]) {
      String string = 
         "Learn Java Programming - knowprogram.com.";
      System.out.println(string);
      System.out.println("Input String length: " 
              + string.length());

      String string1 = string.indent(5);
      System.out.println(string1);
      System.out.println("New String length: " 
              + string1.length());

      String string2 = string.indent(0);
      System.out.println(string2);
      System.out.println("New String length: " 
              + string2.length());

      String string3 = string.indent(-3);
      System.out.println(string3);
      System.out.println("New String length: " 
              + string3.length());
   }
}

Output:-

Learn Java Programming – knowprogram.com.
Input String length: 41
Learn Java Programming – knowprogram.com.

New String length: 47
Learn Java Programming – knowprogram.com.

New String length: 42
Learn Java Programming – knowprogram.com.

New String length: 42

In sttring1 we have used string.indent(5) where 5 > 0 therefore 5 spaces are added to the beginning of the line, and the line ends with a newline character. In string2 we have used indent(0) so it only added a newline character at the end of the line.

In string3 we have used indent(-3) but the string doesn’t have white space at the beginning of the line, therefore, it produces the same result as calling indent(0) on the given string. If the string contains white space at the beginning of the line in that case it will remove 3 white spaces from the beginning of each line.

Adding Indent In String Java

Let us see a few more examples of the String indent() method in Java.

public class Main {
   public static void main(String args[]) {
      String string = "     Hello World    \n"
                    + "     Know Program     ";
      System.out.println("Input String length: " 
                    + string.length());
      System.out.println(string);

      String string1 = string.indent(3);
      System.out.println("String1 length: " 
                    + string1.length());
      System.out.println(string1);

      String string2 = string.indent(0);
      System.out.println("String2 length: " 
                    + string2.length());
      System.out.println(string2);

      String string3 = string.indent(-5);
      System.out.println("String3 length: " 
                    + string3.length());
      System.out.println(string3);
   }
}

Output:-

Input String length: 43
Hello World
Know Program
String1 length: 50
Hello World
Know Program

String2 length: 44
Hello World
Know Program

String3 length: 34
Hello World
Know Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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