Tushar Ahirrao
Tushar Ahirrao

Reputation: 13135

Compare String in flex

I want to check whether string is empty or not

when i create object=Shared.getLocal("abc");

it assigns undefinded to the object at the first time

 if(object.data.name=="undefnied") {
         // is this correct   
 }   

Upvotes: 0

Views: 6085

Answers (4)

jeremym
jeremym

Reputation: 97

To answer your exact question (if is empty), I'd do this:

var name : String = object.data.name;
if(name != null && name.length > 0) {
    //also, a common actionScript technique is to say if(name && name.length...)
    //same difference.
}

Upvotes: 0

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95629

Use the hasOwnProperty function to test if the variable exists. For example:

    if ( object.data.hasOwnProperty("name") ){
        // Ok. object.data.name exists...
        var value_of_name : String = String(object.data["name"]);

        // Check for non-null, non-empty
        if ( value_of_name ){
             // Ok. It is a non-null, non-empty string
             // ...
        }
     }

Upvotes: 2

Aaron
Aaron

Reputation: 4614

undefined is a value, not a string to compare to. You want:

if (object.data.name == undefined) {
    //This property on your SharedObject was/is not defined.
}

Note that setting a property on a SharedObject to null does not delete it, it must be deleted with "delete".

Upvotes: 2

YOU
YOU

Reputation: 123897

I am not sure with flex, but it should be undefined or null without quotes, I think.

Upvotes: 0

Related Questions