Skip to content


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.

Posted in Code.

Tagged with .


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Almog says

    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

  2. kerk says

    @Almog

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

  3. Almog Kurtser says

    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.



Some HTML is OK

or, reply to this post via trackback.



close