10

Is there a static property in Action similar to that in the String object in .net to check if a string is empty, that is String.Empty.

Thanks

3 Answers 3

33

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."

Sign up to request clarification or add additional context in comments.

3 Comments

Me too :S, It is emportant not to compare against "" so as not to create unnecessary strings
This works indeed. Look at the paragraph casting to boolean here help.adobe.com/en_US/as3/learn/…
Bellissimo! The most elegant solution :)
5

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)){
   ...
}

1 Comment

It does not answer exactly the question, but is useful nonthelesss.
4

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.

1 Comment

Thanks, actually I need only to check if empty or null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.