Testing if an attribute value is empty (i.e. not set)

An interesting re-learn from recent forum work is that $String.empty, as a more concise alternative to $String=="", has been around since v5.7.0 (!).

These are the same:

if($Text==""){...
if($Text.empty){...

as are:

if($Text!=""){...
if(!$Text.empty){...

Though $Text.empty is more expressive when scanning code by eye, for the opposite, I think $Text!="" is more expressive as it is easly to miss the prefixed ! in the other version.

As both forms work the same use what makes mote sense to you.

Note, the same operator also works with Lists, Set and Directory data types.

1 Like