Java core learning path Level 1 This level · 23 of 34
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.

Could you say this in an interview?
"  ".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 (2)
  1. String Javadoc (look for "@since 11")docs.oracle.com
  2. Files Javadoc (readString)docs.oracle.com
Previous
Next