java12 introduced four methods to java.lang.String class.

  • indent()
  • transform()
  • describeConstable()
  • resolveConstantDesc()

java12 String indent method

Java12 indent method adds the indentation with given spaces and returns the string.

Here is an example

public class Java12StringTest {
    public static void main(String[] args) {
        String str = "one\ntwo\nthree ";
        System.out.println(str.indent(10));
    }
}

Output:

          one
          two
          three

java12 String transform method

transform method takes Function and applies this to a string and returns the result.

Here is a syntax

    public <R> R transform(Function<? super String, ? extends R> f)

Here is an example to convert a string to lowercase, and remove or strip white spaces from a given string.

public class Java12StringTest {
    public static void main(String[] args) {
        String str = " Hello world ";
        String result=str.transform(String::toLowerCase);
        System.out.println(result);

        String result1=str.transform(String::strip);
        System.out.println(result1);
    }
}

Output:

 hello world
Hello world

java 12 String describeConstable method

java 12 releases with Constable, and It contains the describeConstable method

    Optional<? extends ConstantDesc> describeConstable();

describeConstable method returns Optional instance of an calling object. string.describeConstable returns Optional<String> object.

Here is an example usage for the describeConstable method.

public class Java12StringTest {
    public static void main(String[] args) {
        String str = " Hello world ";
        String result=str.describeConstable().get();
        System.out.println(result);

    }
}

java 12 String resolveConstantDesc method

java 12 releases with ConstantDesc and It contains resolveConstantDesc method

    Object resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;

resolveConstantDesc method returns calling object. resolveConstantDesc returns String object.

Here is an example usage for the resolveConstantDesc method.

import java.lang.constant.Constable;
import java.lang.constant.ConstantDesc;
import java.lang.invoke.MethodHandles;

public class Java12StringTest {
    public static void main(String[] args) {
        String str = " Hello world ";
        String result=str.resolveConstantDesc(MethodHandles.lookup());
        System.out.println(result);
    }
}