Check if a string is empty in action script, similar to String.Empty in .net

The following will catch all of these:

  1. NULL
  2. empty string
  3. whitespace only string

import mx.utils.StringUtil;

var str:String

if(!StringUtil.trim(str)){
   ...
}

You can simply do:

if(string) 
{
    // String isn't null and has a length > 0
}
else
{
   // String is null or has a 0 length
}

This works because the string is coerced to a boolean value using these rules:

String -> Boolean = "false if the value is null or the empty string ( "" ); true otherwise."


You can use length but that is a normal property not a static one. You can find here all the properties of of the class String. If length is 0 the string is empty. So you can do your tests as follows if you want to distinguish between a null String and an empty one:

if (!myString) {
   // string is null
} else if (!myString.length) {
   // string is empty
} else {
   // string is not empty
}

Or you can use Richie_W's solution if you don't need to distinguish between empty and null strings.