开发者

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 t开发者_C百科he first time

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


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
             // ...
        }
     }


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


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


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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜