Tim’s
Bandwagon

ActionScript trim function

ActionScript 2.0 doesn’t have a trim function to strip whitespace from the start and end of a string. Here’s a simple function I wrote because I couldn’t find a satisfactory example on Google!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function trim(str:String):String
{
    var stripCharCodes = {
        code_9  : true, // tab
        code_10 : true, // linefeed
        code_13 : true, // return
        code_32 : true  // space
    };
    while(stripCharCodes["code_" + str.charCodeAt(0)] == true) {
        str = str.substring(1, str.length);
    }
    while(stripCharCodes["code_" + str.charCodeAt(str.length - 1)] == true) {
        str = str.substring(0, str.length - 1);
    }
    return str;
}

The horrible "code_" prefix hack is there because I don’t think there’s a native way to search an array or object for a key or value in ActionScript.

Update: just found this function at: frogstyle.ch. This one does a similar thing, but strips more characters.

1
2
3
4
5
6
function trim(str:String):String
{
    for(var i = 0; str.charCodeAt(i) < 33; i++);
    for(var j = str.length-1; str.charCodeAt(j) < 33; j--);
    return str.substring(i, j+1);
}

Update: as3corelib has a static trim method in com.adobe.utils.StringUtil.

26 Comments

  1. This function has been a great help. Thanks!!!

  2. Thanks for valuable fucntion …it saves lot of time to write again !

  3. Thanks man!

  4. It worked for me. Great Code. Thanks for sharing.

  5. Thanks! This is short and clean: The way it should be.

  6. Thanks a lot! It really helps and works from the forst eseye! Спасибо

  7. Thks for this good code snippet!

  8. Thanks , nice trim methods.

  9. Hi Tim, great post…

    I just want to enhance your code since using loop counter outside loop block is unusual practice, i know, it’s working on flex but we don’t know if someday adobe fix those problem.

    protected function trim(str : String) : String
    {
      var i : int = 0;
      var j : int = 0;
    
      for (i = 0; str.charCodeAt(i) < 33; i++);
      for (j = str.length - 1; str.charCodeAt(j) < 33; j--);
    
      return str.substring(i, j+1);
    }
  10. Thanks for Greate help

  11. There is a static method in mx.utils.StringUtil class: StringUtil.trim()

  12. Re #11. Rubi.

    I’ve done a little searching and the mx.utils.StringUtil class doesn’t exist in the classes distributed with my version of Flash (CS3). I believe it is part of the Flex library.

    A quick google also revealed a class in as3corelib (com.adobe.utils.StringUtil) which also has a static trim method.

    Using either of these is probably better than using my function!

  13. A few quick mods:

    1. I find it much more convenient in ECMAScript to have trim overloaded on the String prototype, as it’s unwaveringly applied to strings.
    2. running a substr on each loop is relatively slow. It’s harder, but faster, to just keep track of position.
    3. I’ve added a way to create the charcodes as needed, so you can, say, strip the ‘/’s off a pathname if you need to.
    String.prototype.trim = function (strip):String {
            if (!!strip)
                strip = " \t\r\n";
            var 
                codes = {},
                i,
                start=0,
                len=this.length;
            for (i=0; i<strip.length; i++)
                codes[strip.charCodeAt(i)]=1;
        while(codes[this.charCodeAt(start)]==1) 
            start++;
        len-=start;
        while(codes[this.charCodeAt(start+len-1)]==1)
            len--;
        return this.substr(start, len);
    }
  14. Huh. Sorry about the formatting…

  15. Re #13, #14.

    Hi Bryan, thanks for posting. I took the liberty of re-formatting your post that the weblog software mangled. Hopefully it looks like you intended?

  16. Thank you very much for this code! saved me time!

  17. Here’s a lightweight regular expression to do it: myString = myString.replace(/^s+|s+$/g, ”);

  18. Re #17

    Hi Anthony, that looks like a good option. It’s worth remembering, though, that support for regular expressions only appears in ActionScript 3.0.

  19. Thanks for this, yours worked, the second didn’t.

  20. Thanks!! You saved my time.

  21. corrected as and works well as … myString.replace(/^\s+|\s+$/g, ”);

  22. This is by far the easiest trim method:

    function trim(str:String) { return str.replace(/^s/, “”).replace(/s$/, “”); }

  23. mx.utils.StringUtil.trim(s:String):String com.adobe.utils.StringUtil.ltrim/rtrim/trim(s:String):String

  24. for(var i : int = 0; str.charCodeAt(i) < 33; i++);
    if(i == str.length){
        return "";
    }

    as i see, without this “if” statement our trim returns full string, if the string is consists with whitespaces only

  25. I just make a change at the last line, because it wasn’t work fine for me.

    public function trim(str:String):String {
        var i:int;
        var j:int;
        var length:int = str.length;
    
        for(i = 0; str.charCodeAt(i) < 33; i++) {}
        for(j = length-1; str.charCodeAt(j) < 33; j--) {}
        return str.substr(i, length-j);
    }
    
  26. thanks a lot Tim. It really helped me about my trouble; I was checking two same strings( one is input the other is from xml) if they are same for as2 too but it seems that they weren’t same. In trace output I saw that one of strings has a hidden “n” in it. So I searched for the trim function and found your blog. thanks a lot again. it really helped me :)

Leave a Comment

Your personal information

Your comment