Hello. I have a new blog.

I've moved this blog to the following URL Kerkness.ca. Thank you for visiting, please update your bookmarks.

Saturday, September 6, 2008

PHP trim() function in Actionscript for Flex

Here is PHP's trim() function reproduced in Actionscript for use in a Flex application

public function trim(str:String):String
{
for(var i:Number = 0; str.charCodeAt(i) < 33; i++);
for(var j:Number = str.length-1; str.charCodeAt(j) < 33; j--);
return str.substring(i, j+1);
}

UPDATE: Thanks to Almog Kurtser, I now know that Flex has a utility to handle trimming strings. Here is an alternative example
import mx.utils.StringUtil;

public function trim(str:String):String
{
return StringUtil.trim(str);
}


Of course it is redundant to actually create a function called trim just to call the method StringUtil.trim() but you get the idea.

3 comments:

  1. Hi,actually Flex has a built in function to trim strings in the StringUtil class.
    http://blog.flexexamples.com/2007/09/07/trimming-strings-using-the-flex-stringutil-classs-trim-method/

    Cheers,
    Almog Kurtser

    ReplyDelete
  2. @Almog

    Ah, Thanks for the tip. I was not aware of the StringUtil class or it's methods. I will update my post.

    ReplyDelete
  3. It's a well camouflaged class indeed!
    I noticed it only when I had to trace down an i18n bug on the ResourceManager.

    btw, nice implementation, in the flex version, they implement it with a switch statement to check whether a character is a whitespace.

    ReplyDelete

Thank you for the comments.