Java String Class indent() Method With Examples Last Updated : 13 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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 Implementation: Example 1 Java // Java Program to illustrate indent() method of // String class // Importing basic libraries import java.io.*; import java.util.*; // Class for indent() method public 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 libraries import java.util.*; import java.io.*; // Class for indent() method public 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 Comment More infoAdvertise with us Next Article Java String Class indent() Method With Examples H hemavatisabu Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Similar Reads Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol 3 min read Field set() method in Java with Examples The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s 4 min read Class getClasses() method in Java with Examples The getClasses() method of java.lang.Class class is used to get the classes of this class, which are the class and interfaces that are public and its members. The method returns the classes of this class in the form of array of Class objects. Syntax: public Class[] getClasses() Parameter: This metho 2 min read Java PatternSyntaxException Class getIndex() Method with Examples Java PatternSyntaxException Class is defined under the java.util.regex package and it depicts unchecked exception that signifies syntax error in a regular expression pattern. This class can be declared using the below syntax, public class PatternSyntaxException extends IllegalArgumentExceptionPatter 2 min read Console format(String, Object) method in Java with Examples The format(String, Object) method of Console class in Java is used to write a formatted string to the output stream of the console. It uses the specified format string and arguments. Syntax: public Console format(String fmt, Object... args) Parameters: This method accepts two parameters: fmt - It re 2 min read List size() method in Java with Examples The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta 2 min read ParsePosition getIndex() method in Java with Example The getIndex() method of java.text.ParsePosition class is used to retrieve the current parse position of this ParsePositon object . Syntax: public int getIndex() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the current parse position of this pa 2 min read StringWriter getClass() method in Java with Examples The getClass() method of StringWriter Class in Java is used to get the parent Class of this StringWriter instance. This method does not accepts any parameter and returns the required Class details. Syntax: public final Class String getClass() Parameters: This method accepts does not accepts any para 2 min read FieldPosition toString() method in Java with Example The toString() method of java.text.FieldPosition class is used to represent the field position object in the form of string. Syntax: public String toString() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the string representation of this FieldPo 2 min read Arrays asList() Method in Java with Examples The Arrays.asList() method in Java is part of the java.util.Arrays class, which is used to convert an array into a fixed-size list. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray(). Example:Below is a simple example of the Arrays as 4 min read Like