Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8200437

String::isBlank

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 11
    • core-libs
    • None
    • minimal
    • Java API
    • SE

      Summary

      Add a new instance method to java.lang.String that returns true if the string is empty or contains only white space, where white space is defined as any codepoint that returns true when passed to Character#isWhitespace(int).

      Problem

      A traditional solution requires the construction of a new string by stripping the instance string of leading or trailing white space.

      Ex 1.
          // true iff is empty or only contains white space
          boolean blank = string.trim().isEmpty();
      

      In Java 8, the in place solution is awkward in its complexity.

      Ex. 2
          // true iff is empty or only contains white space
          boolean blank = string.codePoints().allMatch(Character::isWhitespace);

      Solution

      The introduction of a new method that avoids any object construction and reduces use complexity.

      Ex 3.
          // true iff is empty or only contains white space
          boolean blank = string.isBlank();
      

      Specification

          /**
           * Returns {@code true} if the string is empty or contains only
           * {@link Character#isWhitespace(int) white space} codepoints,
           * otherwise {@code false}.
           *
           * @return {@code true} if the string is empty or contains only
           *         {@link Character#isWhitespace(int) white space} codepoints,
           *         otherwise {@code false}
           *
           * @see Character#isWhitespace(int)
           *
           * @since 11
           */
          public boolean isBlank() {

            jlaskey Jim Laskey
            jlaskey Jim Laskey
            Stuart Marks, Sundararajan Athijegannathan, Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: