Java 11 - New Methods in java.lang.String

Java 11, added various new methods in java.lang.String:

  • String strip(): Returns a string whose value is this string, with all leading and trailing whitespace removed.
  • String stripLeading(): Returns a string whose value is this string, with all leading whitespace removed.
  • String stripTrailing(): Returns a string whose value is this string, with all trailing whitespace removed.
  • boolean isBlank(): Returns true if the string is empty or contains only white space codepoints, otherwise false.
  • Stream lines(): Returns a stream of lines extracted from this string, separated by line terminators.
  • String repeat(int): Returns a string whose value is the concatenation of this string repeated count times.

There are several useful new methods here which can change our way when working with String. Let's take a look...

String::strip()

StringStrip.java
package com.dariawan.string;

public class StringStrip {

    public static void main(String[] args) {
        String s = "	 Java 11 features  ";
        System.out.printf("String: \"%s\"%n", s);
        
        String striped = s.strip();
        System.out.printf("strip(): \"%s\"%n", striped);

        String stripLeft = s.stripLeading();
        System.out.printf("stripLeading(): \"%s\"%n", stripLeft);

        String stripRight = s.stripTrailing();
        System.out.printf("stripTrailing(): \"%s\"%n", stripRight);
        
        String trimmed = s.trim();
        System.out.printf("trim(): \"%s\"%n", trimmed);
        System.out.println("striped.equals(trimmed): " + striped.equals(trimmed));
        System.out.println("Reason: ");
        for (char c : s.toCharArray()) {
            System.out.printf("'%s' ", String.valueOf((int) c));
        }
    }
}
                    

Will produce following output:

String: " Java 11 features? " strip(): "Java 11 features" stripLeading(): "Java 11 features? " stripTrailing(): " Java 11 features" trim(): "Java 11 features?" striped.equals(trimmed): false Reason: '9' '32' '74' '97' '118' '97' '32' '49' '49' '32' '102' '101' '97' '116' '117' '114' '101' '115' '8195' '32'

You can see from the example above that String::strip and String::trim produced different result. This is because trim() only returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).

There are three different whitespaces character in String " Java 11 features ":

  • character tabulation: U+0009 (9)
  • space: U+0020 (32)
  • em space: U+2003 (8195)

Since trim() only remove any character whose codepoint is less than or equal to 'U+0020', in our case U+0009 and U+0020 will be removed, but U+2003 remain. All the whitespace characters are defined by Character::isWhitespace​(char) and/or Character::isWhitespace​(int) which is also defined String::isBlank()

String::isBlank()

StringBlank.java
package com.dariawan.string;

public class StringBlank {

    public static void main(String[] args) {
        String s = "  ";
        // isEmpty() method
        System.out.println(s.isEmpty());
        // isBlank() method
        System.out.println(s.isBlank());
        System.out.println("Characters: ");
        for (char c : s.toCharArray()) {
            System.out.printf("'%s' ", String.valueOf((int) c));
        }
    }
}
                    

And the output is:

false true Characters: '32' '8197'

isBlank() also able to detect four-per-em space or U+2005 (decimal: 8197)

String::lines()

Using this new method, we can easily split a String instance into a Stream<String> of separate lines, separated by line terminators :

StringLines.java
package com.dariawan.string;

public class StringLines {

    public static void main(String[] args) {
        String s = "I eat every fruit.\rFruit is good for health.\r\nBut "
                + "I like durian most.\nDurian is "
                + "the king of fruits";
        s.lines().forEach(System.out::println);
    }
}
                    

A line terminator is one of the following: a line feed character "\n" (U+000A), a carriage return character "\r" (U+000D), or a carriage return followed immediately by a line feed"\r\n" (U+000D U+000A).

String::repeat(int)

This is maybe the coolest additions to the String API in Java 11 release. String::repeat(int) allows concatenating a String with itself a given number of times:

StringRepeat.java
package com.dariawan.string;

public class StringRepeat {

    public static void main(String[] args) {
        String s = "pong ping";
        System.out.printf("Repeat 5: \"%s\"%n", s.repeat(5));
        System.out.printf("Repeat 0: \"%s\"%n", s.repeat(0));
        System.out.printf("Repeat 0: \"%s\"%n", s.repeat(-5));
    }
}
                    

With output:

Repeat 5: "pong pingpong pingpong pingpong pingpong ping" Repeat 0: "" Exception in thread "main" java.lang.IllegalArgumentException: count is negative: -5 at java.base/java.lang.String.repeat(String.java:3149) at com.dariawan.string.StringRepeat.main(StringRepeat.java:9)

Repeat 0 times will return empty String, and repeat -5 will throw IllegalArgumentException "count is negative..."