Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1148

StringUtils.isBlank does not work correctly with strings containing non-breakable whitespace characters

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.6
    • 4.0
    • lang.*
    • Windows 8.1 x64 , Java 1.8, but can be reproduced in any environment with an official Oracle JDK or JRE

    Description

      isBlank uses java.lang.Character.isWhitespace(char ch) method, which has not been changed for a long time for backward compatibility. Over the years non-breakable whitespaces were introduced and are now used in some cases. So if we execute the code

      org.apache.commons.lang.StringUtils.isBlank("\u00A0"); //returns false
      org.apache.commons.lang.StringUtils.isBlank("\u202F"); //returns false
      org.apache.commons.lang.StringUtils.isBlank("\u2007"); //returns false

      we will get 3 falses, which is not right, according to StringUtils.isBlank documentation:

      Checks if a String is whitespace, empty ("") or null.

      I suggest fixing it by using regex pattern

      "^[\\p{Z}]*$"

      instead of looping over the string characters. I know that it is a bit less fast than it works now, but it will work much more correctly. I would be glad to do it myself and write unit tests for it, so if you want, please contact me via email alex.bohush@gmail.com

      Additionally, I would modify the documentation itself too, because it does not tell that it returns true when meeting multiple whitespaces.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              alexbogush Aleksandr Bogush
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

                  Estimated:
                  Original Estimate - 3h
                  3h
                  Remaining:
                  Remaining Estimate - 3h
                  3h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified