Free Extension: AS3 String Utils

To continue the Extension goodness, here is an uber-handy AS3 class that we built the second we had access to AS3.

VI. AS3 String Utils

With the advent of regex in Actionscript, string methods become easier, and in some cases a lot faster. It also gave us a good exercise in AS3. This class is found in the com.chewtinfoil.utils package.
Oddly, Macromedia released a string utils class around the same time we built this, but we found that not only was there less methods, but they made no use of regex, and basically ported an AS2 version.
We have intended on releasing this one for some time, so without further ado, here it is:
Download this AS3 String Utils!
Please let us know if you find any problems, or have any other ideas for fancy string-abusing methods. See a complete API and example after the jump!


import com.chewtinfoil.utils.StringUtils;
var str:String = "This is some <b>Text</b> that contains <i>HTML Characters</i>.";
var newStr:String = StringUtils.stripTags(str);
// This is some Text that contains HTML Characters.


All these static methods are documented in the class using ASDoc-style comments.
Props to Ryan for a super utility!

Lanny McNie

As the dev mgr, Lanny oversees the technical day-to-day details of production and makes sure things get out the door.



  1. Thanks Lanny.

    Also thanks to Stacey for “announcing” it at FITC this year 🙂

    There are more utility classes in the works, including Date & Array Utils.. we’ll try and keep you all posted.

  2. Thanks Grant. I was at Stacey’s session and I thought it sounded pretty good.


  3. Bugs! I think you need to escape more things in more places, because you’re often using passed-in Strings to directly construct a RegExp.

    For example:

    var price :String = “$40”;

    trace(StringUtils.beginsWith(price, “$”));

  4. @Ray: Fixed that and updated the zip, lemme know if you find others.

    @Randy: No Problemo, hope you can put them to good use.

  5. I had created a Number utils class a while back and was nearing the point where a String utils class was necessary. This solves it–thanks!

  6. Phillip Kerman April 25, 2007 at 7:16pm

    The capitalize method doesn’t work as I expected. That is, when I pass true for the second parameter I don’t get all words capitalized.

  7. @Phillip: All fixed, looks like some of the AS3 API’s I used changed from when I originally put this class together.

    Get the updated zip everyone!

  8. AKA the gString class. 🙂

  9. I just downloaded the class and ran a compile. I got the following errors:

    Warning: 3596: Duplicate variable definition. @ line 279


    Warning: 1012: Variables of type uint cannot be undefined. The value undefined will be type coerced to uint before comparison. @ line 642

    These don’t break my application but I thought I would bring it to your attention. By the way, great class thanks for posting it!

  10. awesome, thanks for sharing – lookin forward to the array and date utils!

  11. I think you want to revise StringUtils.endsWith():

    p_string.indexOf(p_end) …



    otherwise, it will return false in cases where it finds the suffix char/s index before the actual suffix itself… for example:

    trace(StringUtils.endsWith(“class”, “s”))

    returns false when it should return true, because it is finding the first “s” and that does not equal the intended comparator position…

    Thanks for the very helpful utils though!

  12. This is a great class. I do get Compiler Errors though, however, it still compiles and runs fine. Here are errors:

    Line 279: Warning 3596 Duplicate variable definition.

    Line 642: Warning 1012 Variables of type uint cannot be undefined. The value undefined will be type coerced to uint before comparison.

  13. @dzedward

    The errors you are describing are simply syntax warnings, and as such have no effect on the performance or behaviour of this class.

    We actually created this class during the “Flash 8 AS3 Preview” days, before the language was complete, and warnings didn’t exist. I will endeavor to post a new version with those warnings removed, as I know how annoying they can be when working in a larger project.


  14. Thanks, that sounds great. Yea, they can get annoying hehe

  15. What if I want to find a word in a string?

  16. i tried to use this extension but recently found one i like better on it creates the code for you and uploads all of the classes on instalation.

  17. Hi Grant – great set of methods there, thanks for sharing them!

    Just one thing that I’d like to see changed in the truncate method – if you give it a long string which equates to just one word, it truncates down to nothing.

    I think it should show all the chars it can, and break the word off with the … trailing.

  18. thanks alot !! It’s very what I want!!! ^^

    Have a good day~!

  19. thanks. great work!

  20. Thanks a lot for the useful extension. It works perfectly!

  21. Thank-you for releasing these functions!

    One thing you need to do: add “|\|” to the end of the regex in escapePattern. Thanks again.

  22. This is a great tool, thanks!

  23. Very useful tools, thank you for hard work!

  24. It works like a charm!

  25. Great, thanks for these rocking Utilz.

    I did find a small typo with ‘removeExtraWhiteSpace’, it should be ‘removeExtraWhitespace’. To many humps.

  26. I found your class by accident, while searching for a trim method in Actionscript (could’ve used String.replace() but I was hoping there was something built-in 🙂 ). So I found your class and I thought it might be useful, but didn’t really feel like including an entire new class coded by someone else into my project (there’s something about ‘I coded this entirely!’ logic I like), but since the class looked like a library I included it in the project (and use the remoteExtraWhitespace() function).

    I then had a need for a function that would return all characters after the last occurrence of a certain character. I implemented this using regex initially and started having a bunch of regex’s all over the place. Overall, I don’t think there is anything wrong with using regexs, but it was so convenient to use your functions! beforeFirst(), afterFirst(), beforeLast(), afterLast() – I felt so spoiled! An added benefit you realize you get only after using a few functions – the code becomes more readable! Regexps are useful, but it takes a few seconds to figure out what is the purpose of that expression (I generally write a comment for that, in case I forget 😉 ). And certainly, writing a comment is a solution, but having the code tell you what it’s doing is so much more elegant.

    Thank you for coding this class and making it free. It made my life a tad easier!

    On another note, I do have 2 requests related to it:

    1. There were 2 trace() commands left in the file somewhere. I deleted them, but you might want to remove them too.

    2. I’m not sure what the purpose of the padRight() function was initially, but I tried to use it to display a list of commands and their descriptions in 2 columns, using a textfield. They all padded correctly, but were misaligned, because (I suspect) the font is not fixed-width, so some characters are wider than the other. I would love to see another function in there that would add output text in columns and would arrange the columns properly, or have the function add as many spaces as necessary to accommodate a certain column width.

    Overall, great job and thanks again for this useful class!

  27. Hi,

    is it possible to find the index(startIndex & endIndex) of specified word from the input text box… for example

    input text box contains : “the world is not enoug”, here the word enough spelling is not correct, will compare the text with some dictionary and need to highlight the wrong words, in this case i need to highlight “enoug”, to highlight the word i need to know the startIndex and endIndex of the word… so how to find out the indexs… any help will be highly appreciated….. Thanks in advance….

  28. Thanks for this v.handy.

  29. thanks for your utils. i have a new function that i often use in conjunction with swfaddress:

    i created a toSEO function to generate a proper url out of something like “Gratis Fürsorge für alle!”. the result is “gratis-fuersorge-fuer-alle”.

    are you interested in adding it to your utils class?

  30. Thanks for this free actionscript resource. Really helps alot.

  31. Awesome class!


    A simple way to do a replace is to just copy the remove method and modify it slightly.

    Here is what I ended up changing it to:

    public static function replace(p_string:String, p_remove:String,p_replace:String = ”, p_caseSensitive:Boolean = true):String {

    if (p_string == null) { return ”; }

    var rem:String = escapePattern(p_remove);

    var flags:String = (!p_caseSensitive) ? ‘ig’ : ‘g’;

    return p_string.replace(new RegExp(rem, flags), p_replace);


  32. This is Brilliant, Many thanks!

  33. Is there a way to use the striptags function but with another parameter. For example to strip everything but the tags?? How would you write a regExp to suit this?

  34. “There are more utility classes in the works, including Date & Array Utils.. we’ll try and keep you all posted.”

    Did you ever get around to posting these? If so, can you reply with a link to them?

    Many thanks and keep up the great work.



  35. Thx, this is a handy usefull class. I found it by accedint as well while I was looking for a trim function that I hoped to be built-in in the string class



  36. For block(), is there a simple way to pass multiple punctuations as the delimeter to handle question marks and exclamation points in addition to periods?

  37. There is a bug in the removeExtraWhitespace function.


    return str.replace(/\s+/g, ‘ ‘);


    return str.replace(/\s+/g, ”);

    Basically the original replaces white space with… white space lol. If you remove the space in between it will work correctly!

  38. Really useful class this! I deal a lot with htmlText for CMS’s – the contentOf function allows me to count any duplicate words but is there any way to write a similar expression that returns an array of the positions (within an htmlText as String() ) of a particular word?

    This would be bloody helpful – any hints or pointers would be greatly appreciated!

  39. Wayne,

    Thanks for the kind words, glad you are getting use out of the class.

    To accomplish what you want you’d need todo a bit more work and iterate over the string searching for your input. Look at the RegExp.exec() docs.

    Simple example:

    var str:String = ‘A sentance with repeating chars’;

    var r:RegExp = new RegExp(‘a’, ‘ig’); // find ‘a’, ignore case..

    var m:Object = r.exec(str); // look for first match

    while (m != null) {

    trace(m, m.index);

    m = r.exec(str);



    A 0

    a 6

    a 20

    a 28

    Hope that helps.

  40. Fantastic!

    You should see some of the (cpu intensive) loops of if’s and while’s I’ve been trying to emulate the same result.

    Can’t thank you enough sir!

  41. Is there a way to include accented char in the function capitalize ?

    like : marie-élaine would become Marie-Élaine

    tx !

  42. this is awesome. saved me a lot of headache. Big Ups!

  43. OMG!!!!!!!!!!!!! I SO OWE YOU BEER!!!!!!!!!!!!!!!!!!!!!!!

  44. Does this method actually work for anyone?


    I get a next infinite loop from this code:

    while( StringUtils.contains( myString , ‘ ‘ ))
    myString = StringUtils.removeExtraWhitespace( myString );

  45. Ahh… I see the fix a couple comments up. To bad this file isn’t in a repo.

  46. how about truncate a String in a Textfield by restricting it to a number of lines?

  47. bug in endsWith()
    endsWith(“aaa”, “1234”) return true
    because -1 = 3 – 4

  48. Good catch, these things have had a few updates since this post, I should really post them.

    return new RegExp(p_end+’$’, ‘ig’).test(p_string);

    That should do the trick.

  49. Also you could just do a simple length check, like such:

    return p_string.length > p_end.length && p_string.indexOf(p_end) == (p_string.length – p_end.length);

  50. I also pushed these classes and some of the others that were mentioned to github a few months back.

    Sorry not advertising!

    p.s. for the removeExtraWhitespace() method it works as designed, its not removing all whitespace just reducing multiple occurrences of white space (double spaces, extra line breaks etc) to a single space. The suggested fixes would make the function removeWhitespace 😉

  51. thanks, nice piece of work! 🙂

  52. I appreciate, cause I found just what I used to be looking for.
    You’ve ended my four day long hunt! God Bless you man. Have a great day. Bye

Comments are closed.