During the development of a feature for the company I work for, I saw the need to validate if a String was empty. The idea was to receive a String expression "cron". When using the method "hasLength (@Nullable String str)" in org.springframework.util.StringUtils (spring-core-5.3.14), I identified that it validates as true even if you pass as an argument something like " ". This is because in the str.isEmpty() return, "trim()" is not applied to remove the space and after that, check if in fact the String passed is empty. My suggestion would be to replace the "!str.isEmpty()" check with "!str.trim().isEmpty()". Thank you all the team for your attention and for the beautiful work with Spring !!!
Comment From: mdeinum
That is what the hasText
method is for not the hasLength
. Both methods serve a different purpose and have done well in the last years. It is also stated in the documentation that it will return a length for a purely whitespace String
Note: this method returns
true
for aString
that purely consists of whitespace.
Comment From: bclozel
Closing as this works as designed. See @mdeinum 's comment.