Easy java java-21

Which new String methods came in Java 11?

The 30 second answer

Java 11 added isBlank, which is true for empty or whitespace-only strings; lines, which gives a stream of lines; strip, stripLeading and stripTrailing, which remove Unicode whitespace; and repeat, which repeats a string n times. The key difference is strip versus the old trim: trim only removes characters up to the plain space code, while strip understands all Unicode whitespace.

"  ".isBlank();                    // true
"a\nb\r\nc".lines().count();       // 3
"\u2003hi\u2003".strip();          // "hi"  (trim() keeps the em spaces)
"ab".repeat(3);                    // "ababab"
  • trim() removes characters with code point up to U+0020.
  • strip() removes characters for which Character.isWhitespace is true.

Nearby additions worth knowing: Files.readString / writeString (Java 11), and indent, transform (Java 12) and stripIndent, translateEscapes, formatted (Java 15).

Sources: String Javadoc (look for "@since 11"), Files Javadoc (readString)

Sources (0)
See inline citations in the answer above.