Jan 10, 2020 in JAVA

Java Strings New Methods

1 Answer

0 votes
Jan 10, 2020
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*****";







  Hello Pankaj

How are you?




      Hello Pankaj

   How are you?




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"


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(","));});



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();







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"


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.

Related questions

0 votes
Mar 16 in JAVA
  • #string-program
  • compare-string
  • string-comparison
  • java-string-comparision
0 votes
Oct 13, 2020 in JAVA
0 votes
Dec 7, 2020 in JAVA