Java 12 String API Updates

Java 12 String API Updates

Want to learn more about the new features coming to Java 12 string APIs?

1. Introduction

Java 12 added a couple of useful APIs to the String class. In this tutorial, we will explore these new APIs with examples.

2. indent()

The indent() method adjusts the indentation of each line of the string based on the argument passed to it.

When indent() is called on a string, the following actions are taken:

  1. The string is conceptually separated into lines using lines(). lines() is the String API introduced in Java 11.
  2. Each line is then adjusted based on the int argument n passed to it and then suffixed with a line feed ā€œ\nā€. If n > 0, then n spaces 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. In case a given line does not contain sufficient white space, then all leading white space characters are removed.If n == 0, then the line remains unchanged. However, line terminators are still normalized.1. The resulting lines are then concatenated and returned.

For example:

public void whenPositiveArgument_thenReturnIndentedString() {
    String multilineStr = "This is\na multiline\nstring.";
    String outputStr = "   This is\n   a multiline\n   string.\n";

    String postIndent = multilineStr.indent(3);

    assertThat(postIndent, equalTo(outputStr));

We can also pass a negative int to reduce the indentation of the string. For example:

public void whenNegativeArgument_thenReturnReducedIndentedString() {
    String multilineStr = "   This is\n   a multiline\n   string.";
    String outputStr = " This is\n a multiline\n string.\n";

    String postIndent = multilineStr.indent(-2);

    assertThat(postIndent, equalTo(outputStr));

3. transform()

We can apply a function to this string using the transform() method. The function should expect a single String argument and produce a result:

public void whenTransformUsingLamda_thenReturnTransformedString() {
    String result = "hello".transform(input -> input + " world!");

    assertThat(result, equalTo("hello world!"));

It is not necessary that the output has to be a string. For example:

public void whenTransformUsingParseInt_thenReturnInt() {
    int result = "42".transform(Integer::parseInt);

    assertThat(result, equalTo(42));

4. Conclusion

In this article, we explored the new String APIs in Java 12. As usual, code snippets can be found over on GitHub.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java String ā€“ String Functions In Java With Examples

In this post, we will be discussing about a new concept, Java String. String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.

String Initialization in Java

Learn about the difference between declaration and initialization of String in Java.

How to write @ as string in Java

How do I write @ as an string in java. When I write "@" it indicate as missing resource type This is my TextView