Archive

Archive for the ‘linkedin’ Category

Parse Boolean value from String…

July 10th, 2009 No comments

One of the small niggles I have with AS3 is the way it parses boolean values from strings. It first tries to convert the string value to a number, then to a boolean (there is a long explanation which I will not go into at this point)… To maintain sanity I have written a very simple function to handle it for me:

static public function parseBoolean(str:String):Boolean
{
    switch(str.toLowerCase())
    {
        // Check for true values
        case "1":
        case "true":
        case "yes":
        return true;

        // Check for false values
        case "0":
        case "false":
        case "no":
        return false;

        // If all else fails cast string
        default:
        return Boolean(str);
    }
}

String.IsNullOrEmpty

May 15th, 2009 No comments

Having a .NET background I frequently used the String.IsNullOrEmpty() method. However after starting with ActionScript I have noticed that this (and a few other) functionalities are missing. So here is my version:

public static function IsNullOrEmpty(value:String):Boolean
{
    // Check if string is 'empty'
    if (value == "")
    {
        return true;
    }

    // Check if string is 'null'
    if (value == null)
    {
        return true;
    }
 
    // Not 'empty' or 'null', it must be ok!
    return false;
}