171

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.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173

4 Answers4

335

Another option is

if (myString?.trim()) {
  ...
}

(using Groovy Truth for Strings)

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • 4
    myString?.trim() returns a string (or null or blank) instead of the question's which returns a boolean. I guess it depends how you're using the output, for just an 'if' it's fine. – Steven Feb 07 '12 at 00:31
  • 17
    or `boolean containsData = myString?.trim()` – tim_yates Feb 07 '12 at 09:22
  • 8
    @Steven, Groovy truth says that a null or empty string is false, so you can just wrap it in `()` as well if you need it in a statement that wouldn't already make it a boolean. For example, `def myVal = (myString?.trim())` – cdeszaq Feb 07 '12 at 13:11
  • 13
    @cdeszaq if you do `def myVal = (myString?.trim())` then `myVal` is either a `String` or `null`, wrapping it in braces doesn't change it to a boolean. However, if you assign it to a boolean variable or use `(myString?.trim()) as boolean` or (as in my answer) use it in an `if` statement, then as you say it will use the Groovy Truth value of the string (or the null) – tim_yates Feb 07 '12 at 13:28
  • 1
    While this works for string, note if it's a number 0 it will fail. Just a point to highlight, if the value is a guaranteed string then it's good – rboy Oct 21 '16 at 22:03
  • What if String is "false"? – Bertl Aug 27 '19 at 08:29
  • 6
    `​if ("false") println "that's true too"` – tim_yates Aug 27 '19 at 10:33
  • 1
    This is supposed to be the answer, but without understanding how this actually works (i.e. what is considered `true` in Groovy) I can't use this. Could you expand the answer explaining _why_ this works in Groovy? – Michiel Haisma Oct 11 '21 at 16:02
  • 2
    @MichielHaisma added a link to the documentation – tim_yates Oct 11 '21 at 16:56
10

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
doelleri
  • 19,232
  • 5
  • 61
  • 65
  • 3
    Where would the best place to put the metaClass modification be? – cdeszaq Feb 07 '12 at 13:09
  • and what if foo = null ? – Bertl Sep 01 '20 at 10:54
  • @Bertl that would be a null pointer exception. But if foo is null it doesn't have a class and also wouldn't have the metaClass method. – doelleri Sep 01 '20 at 20:10
  • Your code is utilizing a double negative and makes the code hard to follow. It's better to do something like isBlank instead. – stricq May 17 '23 at 15:45
  • Please don't do something like this for a method this simple unless it is absolutely necessary, no one likes random methods on classes that are specific to your codebase. At the very least, in this case name it getNotAllWhiteSpace() to tie it back to the underlying implementation in this case where you are just effectively renaming and inverting a method that already exists. It isn't that hard to just use !foo.allWhitespace to begin with. – Michael Peterson Aug 19 '23 at 03:40
3

It's works in my project with grails2.1:

String str = someObj.getSomeStr()?.trim() ?: "Default value"

If someObj.getSomeStr() is null or empty "" -> str = "Default value"
If someObj.getSomeStr() = "someValue" -> str = "someValue"

in_trance
  • 31
  • 3
2

I find this method to be quick and versatile:

static boolean isNullOrEmpty(String str) { return (str == null || str.allWhitespace) } 

// Then I often use it in this manner
DEF_LOG_PATH = '/my/default/path'
logPath = isNullOrEmpty(log_path) ? DEF_LOG_PATH : log_path

I am quite new to using groovy, though, so I am not sure if there exists a way to make it an actual extension method of the String type and this works well enough that I have not bothered to look.

Thanks, -MH

MostHated
  • 151
  • 2
  • 11