Java String Class indent() Method With Examples

13 Apr, 2021
JDK 12 introduced indent() method in Java.lang.String class. This method is useful to add or remove white spaces from the beginning of the line to adjust indentation for each string line.
Syntax:
public String indent(int n)
Parameter: It takes integer n as input and does indentation accordingly. 
Also, each line is suffixed with “\n” (a newline character).
Procedure: 
When a string is provided to indent() method, 
It calls lines() functionThen, for each line, does indentation based on integer value provided as per user cases discussed below:If n>0 (Positive)Then n white spaces are added at the starting of each line and each line is suffixed with “\n”.If n==0Then the indentation remains as it is, only line is suffixed with “\n”.If n<0 (Negative), thenIf (+n) > leading white spaces availableThen all leading white spaces are removed for each line and each line is suffixed with “\n”If (+n) < leading white spaces availableThen (+n) leading white spaces are removed for each line and each line is suffixed with “\n”Then, suffix each line with “\n”.Then, concatenates resulting string lines and returns
It calls lines() function
Then, for each line, does indentation based on integer value provided as per user cases discussed below:If n>0 (Positive)Then n white spaces are added at the starting of each line and each line is suffixed with “\n”.If n==0Then the indentation remains as it is, only line is suffixed with “\n”.If n<0 (Negative), thenIf (+n) > leading white spaces availableThen all leading white spaces are removed for each line and each line is suffixed with “\n”If (+n) < leading white spaces availableThen (+n) leading white spaces are removed for each line and each line is suffixed with “\n”
If n>0 (Positive)Then n white spaces are added at the starting of each line and each line is suffixed with “\n”.
Then n white spaces are added at the starting of each line and each line is suffixed with “\n”.
If n==0Then the indentation remains as it is, only line is suffixed with “\n”.
Then the indentation remains as it is, only line is suffixed with “\n”.
If n<0 (Negative), thenIf (+n) > leading white spaces availableThen all leading white spaces are removed for each line and each line is suffixed with “\n”If (+n) < leading white spaces availableThen (+n) leading white spaces are removed for each line and each line is suffixed with “\n”
If (+n) > leading white spaces availableThen all leading white spaces are removed for each line and each line is suffixed with “\n”
Then all leading white spaces are removed for each line and each line is suffixed with “\n”
If (+n) < leading white spaces availableThen (+n) leading white spaces are removed for each line and each line is suffixed with “\n”
Then (+n) leading white spaces are removed for each line and each line is suffixed with “\n”
Then, suffix each line with “\n”.
Then, concatenates resulting string lines and returns
Implementation:
Example 1
Java
// Java Program to illustrate indent() method of// String class // Importing basic librariesimport java.io.*;import java.util.*; // Class for indent() methodpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Custom input string        String input            = "GeeksforGeeks\nA Computer Science portal for geeks.";         // Print and display the input string        System.out.println(input);         // Print the above string length        // using standard length() method        System.out.println("Input String length: "                           + input.length());         // Now, calling the indent() method        // for random value of N         // Case 1: N>0 | Positive        // Say N = 5 which is positive        // so as per procedural algorithm        // 5 white spaces are added        // at the starting of each line        String output = input.indent(5);         // Print and display output string        System.out.println(output);         // Print the new string length        // again using the length() method        System.out.println("New String length: "                           + output.length());         // Case 2: N=0 | Zero        // Call indent method with n=0        String output1 = input.indent(0);        System.out.println(output1);        System.out.println("New String length: "                           + output1.length());         // Case 3: N < 0 | Negative        // Call indent method with n=-3 (negative)        String output2 = input.indent(-3);         // Print the output string        System.out.println(output);         // Print output(new) string length        System.out.println("New String length: "                           + output2.length());    }}
Output:
GeeksforGeeks
A Computer Science portal for geeks.
Input String length: 50
    GeeksforGeeks
    A Computer Science portal for geeks.

New String length: 61
GeeksforGeeks
A Computer Science portal for geeks.

New String length: 51
GeeksforGeeks
A Computer Science portal for geeks.

New String length: 51
Example 2:
Java
// Java Program to illustrate indent() method of// String class // Importing basic librariesimport java.util.*;import java.io.*; // Class for indent() methodpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Input string        String input = "GeeksforGeeks";        System.out.println(input);        System.out.println("Input String length: "                           + input.length());         // Call indent method on input string with n=5        // (positive)        String output = input.indent(5);        System.out.println(output);        System.out.println("New String length: "                           + output.length());         // Call indent method on output string with n=0        String output1 = output.indent(0);        System.out.println(output1);        System.out.println("New String length: "                           + output1.length());         // Call indent method on output1 string with n=-3        // (negative)        String output2 = output.indent(-3);        System.out.println(output2);        System.out.println("New String length: "                           + output2.length());    }}
 
 
Output:
 
GeeksforGeeks
Input String length: 13
    GeeksforGeeks

New String length: 19
    GeeksforGeeks

New String length: 19
 GeeksforGeeks

New String length: 16
 
simmytarika5
Java-Strings
Picked
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java