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.
Published at 4:26 pm on July 23rd, 2007.
Topics: Flash
22 Comments
This function has been a great help. Thanks!!!
Comment by yiannis karadimas at 7:19 pm on February 10th, 2008 #link
Thanks for valuable fucntion …it saves lot of time to write again !
Comment by Baljeet at 6:20 pm on February 12th, 2008 #link
Thanks man!
Comment by yet another AS2 looney at 1:25 pm on March 11th, 2008 #link
It worked for me. Great Code. Thanks for sharing.
Comment by Suresh at 8:39 pm on August 9th, 2008 #link
Thanks! This is short and clean: The way it should be.
Comment by Greg Ferrell at 8:41 pm on August 21st, 2008 #link
Thanks a lot! It really helps and works from the forst eseye! СпаÑибо
Comment by quezak at 1:52 pm on August 28th, 2008 #link
Thks for this good code snippet!
Comment by pok at 6:09 pm on September 4th, 2008 #link
Thanks , nice trim methods.
Comment by Brent at 2:39 pm on October 7th, 2008 #link
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); }Comment by Denny at 7:46 am on December 16th, 2008 #link
Thanks for Greate help
Comment by Jitendra Panwar at 8:31 am on December 17th, 2008 #link
There is a static method in mx.utils.StringUtil class: StringUtil.trim()
Comment by Rubi at 1:30 pm on January 7th, 2009 #link
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!
Comment by Tim at 4:29 pm on January 7th, 2009 #link
A few quick mods:
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); }Comment by Bryan Elliott at 4:08 pm on January 30th, 2009 #link
Huh. Sorry about the formatting…
Comment by Bryan Elliott at 4:08 pm on January 30th, 2009 #link
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?
Comment by Tim Brayshaw at 4:34 pm on January 30th, 2009 #link
Thank you very much for this code! saved me time!
Comment by AMeen at 6:23 am on February 4th, 2009 #link
Here’s a lightweight regular expression to do it: myString = myString.replace(/^s+|s+$/g, ”);
Comment by Anthony Sapien at 10:43 pm on March 9th, 2009 #link
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.
Comment by Tim at 9:47 am on April 10th, 2009 #link
Thanks for this, yours worked, the second didn’t.
Comment by Eduardo Vaz de Mello at 8:20 pm on April 18th, 2009 #link
Thanks!! You saved my time.
Comment by Satish at 11:33 am on May 13th, 2009 #link
corrected as and works well as … myString.replace(/^\s+|\s+$/g, ”);
Comment by anonymous at 2:31 pm on October 7th, 2009 #link
This is by far the easiest trim method:
function trim(str:String) { return str.replace(/^s/, “”).replace(/s$/, “”); }
Comment by Aaron at 9:40 pm on January 21st, 2010 #link