+1 vote
in JAVA by
edited by

Java Strings New Methods

1 Answer

0 votes
by
edited by
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.

Related questions

0 votes
asked Jan 10, 2020 in JAVA by DavidAnderson
0 votes
asked May 29, 2020 in JAVA by anonymous
...