Java String Class indent() Method With Examples
Last Updated :
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() 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==0
- Then the indentation remains as it is, only line is suffixed with “\n”.
- If n<0 (Negative), then
- If (+n) > leading white spaces available
- Then all leading white spaces are removed for each line and each line is suffixed with “\n”
- If (+n) < leading white spaces available
- 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
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[])
{
String input
= "GeeksforGeeks\nA Computer Science portal for geeks." ;
System.out.println(input);
System.out.println( "Input String length: "
+ input.length());
String output = input.indent( 5 );
System.out.println(output);
System.out.println( "New String length: "
+ output.length());
String output1 = input.indent( 0 );
System.out.println(output1);
System.out.println( "New String length: "
+ output1.length());
String output2 = input.indent(- 3 );
System.out.println(output);
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
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String args[])
{
String input = "GeeksforGeeks" ;
System.out.println(input);
System.out.println( "Input String length: "
+ input.length());
String output = input.indent( 5 );
System.out.println(output);
System.out.println( "New String length: "
+ output.length());
String output1 = output.indent( 0 );
System.out.println(output1);
System.out.println( "New String length: "
+ output1.length());
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
Share your thoughts in the comments
Please Login to comment...