What’s New In Java 11 And How To Use String

Java String Main Logo

What’s New In Java 11 And How To Use String

Hello! Since the release of Java 11 have passed a day, and now finally began to appear the first release reviews. I will devote my small article to the inconspicuous for official releases and therefore to the attention paid update of the String class, especially since it is not mentioned in the official documentation of the 11th Java.

  • Indeed, if we look at the String class, then among the many familiar methods we find a few, labeled as “@since 11“. And yes, officially in Java they appeared only yesterday.

Of course, there may well be doubts in the utility of each of the functions, since the most useful and necessary functions have already been written in previous versions of Java, but some of these can come in handy. The article was small, but this is not only my fault but also the fault of Oracle – they included only 4 (+2) methods in the release, which, of course, is not enough.

Let’s get started

strip ()

This method removes all spaces before the first non-blank space and after the last one.

For example:


String withSpaces = " a ";
String withoutSpaces = withSpaces.strip();

String OUTPUT_TEMPLATE = "<%s>"
System.out.println(String.format(OUTPUT_TEMPLATE, withSpaces));
System.out.println(String.format(OUTPUT_TEMPLATE, withoutSpaces));

The result displayed on the screen will be:


original: < a >
strip: <a>

The strip () method has two cousins – stripLeading () and stripTrailing (). The first – remove spaces only in front, before the first non-blank. The second is just behind.


String leading = withSpaces.stripLeading();
String trailing = withSpaces.stripTrailing();

We get the result:


stripLeading: <a >
stripTrailing: < a>

isBlank ()

The method returns the result of the query, whether the string contains any characters other than spaces.

That is if we execute this code:


String blank = " ";
Boolean isBlank = blank.isBlank();

The result will be:


true

Within the method itself, there are two implementations – for Latin characters and for a string in the UTF-16 encoding.


public boolean isBlank() {
return indexOfNonWhitespace() == length();
}

private int indexOfNonWhitespace() {
if (isLatin1()) {
return StringLatin1.indexOfNonWhitespace(value);
} else {
return StringUTF16.indexOfNonWhitespace(value);
}
}

repeat ()

This method copies the contents of the string a specified number of times and returns the result in one line.

For example, by executing the code:


String sample = "(^_^) ";
String multiple = sample.repeat(10);

We will get:


(^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^)

If the number of iterations is zero, then the line will not contain any symbols at all.


String blank = sample.repeat(0);

Result:


length: 0

lines ()

It would be strange to expect from Oracle that they will issue an update to String, not including any implementation of the Stream API in the class. And they did include the functionality in the String class.

The lines method converts all lines of a string to the corresponding Stream. It looks like this:


String lines = "Blind Text Generator is a useful tool\n" +
"which provides Lorem Ipsum and a number of alternatives.\n" +
"The number of characters, words, and paragraphs\n" +
"are easily controlled and you can set \n" +
"the font to appreciate how it’ll look in your design.";

lines
.lines()
.map(l -> "next line: " + l)
.forEach(System.out::println);

We obtain the result:


next line: Blind Text Generator is a useful tool
next line: which provides Lorem Ipsum and a number of alternatives.
next line: The number of characters, words, and paragraphs
next line: are easily controlled and you can set
next line: the font to appreciate how it’ll look in your design.

We got a full stream, with which we can then do everything that we usually do with streams usual. The application to this can be very different, and I would hope that such a feature will be warmly accepted by the developers.

If we look inside the method itself, we’ll see that two splitters are used to convert a string into Stream, depending on which encoding the string is.


public Stream<String> lines() {
return isLatin1() ? StringLatin1.lines(value)
: StringUTF16.lines(value);
}

On this list of innovations in the release of the part String ends. If I missed something, I will be happy to learn about it and add it to the review.