Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, May 27, 2019

Java 12 String API indent​() Method Example

1. Overview

In this quick tutorial, We'll learn new indent() introduced in JDK 12 in Java.lang.String class.

Demonstrated with examples how to use indent() method.

Please read article on Java 12 new String API methods addition.


Will go through its internal code and how it works.

Java 12 String indent​() Method Example

2. indent(int n)

As name suggests, indent() method adds or removes the white spaces at the beginning of the string for provided n value and adds '\n' at the end of line. This method takes int argument n.

Based on the n value, indentation will be changed.

If n value is positive then it adds n white spaces at beginning.
If n value is negative then removes n white spaces from beginning if white spaces are available.
If n value is 0 then input string remains unchanged.

If input string has line terminations (\n) then it applies to the all lines.

2.1 Syntax

public String indent​(int n)

This is a public method and must be accessed on String instance. Returns new indented String.

2.2 Arguments

n - number of leading white space characters to add or remove

3. Example

We will write a few example programs on indent method for each case of value n.

Consider the following is the input.

String input = "I am Venkatesh";

3.1 n positive value

Passing n value as 5 which is positive. Hence, it adds 5 blank spaces at the beginning of input string.

String output1 = input.indent(5);

Output:

In the output, both input and output are printed. Observe 5 white spaces are added and added '\n' at end.

I am Venkatesh
     I am Venkatesh

Now will see length of both values.

System.out.println(input.length());
System.out.println(output1.length());

Output:

14
20

input length is 14 and added 5 spaces to output1. Now output1 should be 15 + 5 = 19 but output1 is 20 because it is added with '\n' at end.       

3.2 n negative value

Invoking indent method on above output1 which is already indented by 5 spaces. Now it should remove those 5 spaces.

String output2 = output1.indent(-5);

Output:

I am Venkatesh


yes, it has removed all white spaces from beginning and adds '\n' at end.

3.3 n value 0

String output3 = input.indent(0);

Output:

I am Venkatesh

Output string unchanged. Not added or removed anything from it.

3.4 Line Terminators

Now assume input string has line terminators such as '\n' or \t. Now intent() method calls internally lines() method on input string. Takes each line from it and apply logic of indent on it. See the below code and it's output.

String lineTerminatorsInput = "Welcome to Java 12 new methods \n 1. indent \n 2.transform \n many";
String lineTerminatorsOutput = lineTerminatorsInput.indent(3);
System.out.println("lineTerminatorsInput : \n"+lineTerminatorsInput);
System.out.println("lineTerminatorsOutput : \n"+lineTerminatorsOutput);

Output:

Added 3 white spaces at beginning of each line separator.

lineTerminatorsInput : 
Welcome to Java 12 new methods 
 1. indent 
 2.transform 
 many
 
lineTerminatorsOutput : 
   Welcome to Java 12 new methods 
    1. indent 
    2.transform 
    many
      

4. Internal Implementation

Let us take a look at internal code and it's working mechanism.

public String indent(int n) {
     return isEmpty() ? "" :  indent(n, false);
 }

 private String indent(int n, boolean removeBlanks) {
     Stream stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE)
                                          : lines();
     if (n > 0) {
         final String spaces = " ".repeat(n);
         stream = stream.map(s -> spaces + s);
     } else if (n == Integer.MIN_VALUE) {
         stream = stream.map(s -> s.stripLeading());
     } else if (n < 0) {
         stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace())));
     }
     return stream.collect(Collectors.joining("\n", "", "\n"));
 }

   
Step 1: First returns "" if input string is empty by calling isEmpty() method

Step 2: Invokes line() method which returns stream of String, separated by line terminators.

Step 3: if n > 0 then adds n empty spaces by calling repeat() method and adds to all lines.
        if n < 0 then removes n white spaces from beginning.
        if n = 0 then it returns string as unchanged (invoking stripLeading() method.)
       
        All these are performed on stream of lines and stored as stream.
       
Step 4: Converts stream to string and returns it.
       

5. Conclusion


In this short article, We've gone through how to add white left spaces to the string using indent() method. Discussed how to add or remove white spaces and what happen if string has line termination characters.

Example program is implemented in this article is available on GitHub.

No comments:

Post a Comment

Please do not add any spam links in the comments section.