11 November 2009

AS2: PHP style trim for Actionscript 2 - with 'any' character support

Theres a few of these functions around but none (that I could find) will allow you to trim non whitespace characters so I pulled this one together which lets you trim any character you want.

//USAGE:
trace(trim(";A test;;", ";"));    //outputs: "A test" 

function trim(str:String, char:String):String{
    if(char==undefined){char=" ";}
    code=char.charCodeAt(0)
    for(var i = 0; str.charCodeAt(i) ==code; i++);
    for(var j = str.length-1; str.charCodeAt(j) ==code; j--);
    return str.substring(i, j+1);
}
Cron Design Studio: Dublin based web design & software development