Groovy adds the isAllWhitespace()
method to Strings, which is great, but there doesn't seem to be a good way of determining if a String has something other than just white space in it.
The best I've been able to come up with is:
myString && !myString.allWhitespace
But that seems too verbose. This seems like such a common thing for validation that there must be a simpler way to determine this.
This question is related to
string
groovy
whitespace
Another option is
if (myString?.trim()) {
...
}
You could add a method to String to make it more semantic:
String.metaClass.getNotBlank = { !delegate.allWhitespace }
which let's you do:
groovy:000> foo = ''
===>
groovy:000> foo.notBlank
===> false
groovy:000> foo = 'foo'
===> foo
groovy:000> foo.notBlank
===> true
Source: Stackoverflow.com