Java 12 String Methods

Filed Under: Java

Java 12 was released in March 2019. There are four new methods added in String class. In this tutorial, we will look into these new methods in detail.

1. indent(int n)

This method adjusts the indentation of each line in the string based on the value of ‘n’ and also normalizes line termination characters.

  • If n > 0, then n spaces (U+0020) are inserted at the beginning of each line.
  • If n < 0, then up to n white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed. The tab character is also treated as a single character.
  • If n = 0, then the line remains unchanged. However, line terminators are still normalized.

String str = "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****";

System.out.println(str.indent(0));
System.out.println(str.indent(3));
System.out.println(str.indent(-3));

Output:


*****
  Hi
  	Hello Pankaj
How are you?
*****

   *****
     Hi
     	Hello Pankaj
   How are you?
   *****

*****
Hi
Hello Pankaj
How are you?
*****

Let’s look into the same examples through jshell.


➜  ~ jshell
|  Welcome to JShell -- Version 12
|  For an introduction type: /help intro

jshell> String str = "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****";
str ==> "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****"

jshell> str.indent(0)
$2 ==> "*****\n  Hi\n  \tHello Pankaj\nHow are you?\n*****\n"

jshell> str.indent(3)
$3 ==> "   *****\n     Hi\n     \tHello Pankaj\n   How are you?\n   *****\n"

jshell> str.indent(-3)
$4 ==> "*****\nHi\nHello Pankaj\nHow are you?\n*****\n"

jshell> 

Notice that \r is being normalized to \n when indent() method is called.

2. transform(Function<? super String,​? extends R> f)

This method allows us to call a function on the given string. The function should expect a single String argument and produce an R result.

Let’s look at an example where we will use transform() method to convert a CSV string to the list of strings. Notice the use of lambda expressions to implement the functional interface.


String s = "Hi,Hello,Howdy";
List strList = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
System.out.println(strList);

Output:

Java 12 String Methods

3. Optional<String> describeConstable()

Java 12 has introduced Constants API in JEP 334. If you look at the String class documentation, it implements two new interfaces from Constants API – Constable, and ConstantDesc. This method is declared in the Constable interface and implemented in the String class.

This method returns an Optional containing the nominal descriptor for this instance, which is the instance itself.


String so = "Hello";
Optional os = so.describeConstable();
System.out.println(os);
System.out.println(os.get());

Output:


Optional[Hello]
Hello
Hello
Java String Method DescribeConstable

Java String Method DescribeConstable

4. String resolveConstantDesc​(MethodHandles.Lookup lookup)

This method is part of Constants API and declared in ConstantDesc interface. It resolves this instance as a ConstantDesc, the result of which is the instance itself.


jshell> import java.lang.invoke.MethodHandles;

jshell> String so1 = "Hello";
so1 ==> "Hello"

jshell> so1.resolveConstantDesc(MethodHandles.lookup());
$18 ==> "Hello"

Conclusion

The indent() and transform() methods are a great addition to the String class. The Constants API methods don’t have much usage for normal development related tasks.

Comments

  1. prd says:

    Hello, I would like to ask, what is the purpose of ‘Optional describeConstable()’? What scenarios can I use specifically? Thank you.

  2. Muralidhar S J says:

    Thanks for putting it together to help seekers sure will gothrough it and will contact you if any clarifications required. Thanks once again

  3. Sumeet Varma says:

    Hello Pankaj,

    Thanks for making this article which is well descriptive and explained However, Please rectify the method name ‘intent()’ to ‘indent()’ under “Conclusion” Section.

    Thanks and Regards,
    Sumeet Varma

    1. Pankaj says:

      Thanks for noticing the typo, I have fixed it.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages