Verify that the string is empty in the action script, similar to String.Empty in .net

Is there a static property in action like the one in the String object in .net to check if the string is empty, this is String.Empty.

thanks

+7
source share
3 answers

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 bound to a boolean using these rules :

String → Boolean = "false if the value is null or an empty string (" "), otherwise true."

+29
source

You can use length , but this is a normal property, not a static one. Here you can find all the properties of the String class. If the length is 0 , the string is empty. Therefore, you can run your tests as follows if you want to distinguish between null String and empty:

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

Or you can use the Richie_W solution if you do not need to distinguish between empty and null strings.

+3
source

The following will catch it all:
1. null
2. blank line
3. line with spaces

 import mx.util.StringUtil; var str:String if(!StringUtil.trim(str)){ ... } 
+2
source

All Articles