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!
Example
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); trace(newStr); // This is some Text that contains HTML Characters.
API
All these static methods are documented in the class using ASDoc-style comments.afterFirst
adterLast
beginsWith
beforeFirst
beforeLast
between
block
capitalize
contains
countof
editDistance
endsWith
hasText
isEmpty
isNumeric
padLeft
padRight
properCase
quote
remove
removeExtraWhiteSpace
reverse
reverseWords
similarity
stripTags
swapCase
trim
trimLeft
trimRight
wordCount
truncate
Props to Ryan for a super utility!

Comments (36)
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.
Posted by: Ryan Matsikas at April 25, 2007 11:58 AMURL: http://gskinner.com
Thanks Grant. I was at Stacey's session and I thought it sounded pretty good.
Posted by: tim at April 25, 2007 12:06 PM-tim.
URL:
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";
Posted by: Ray Greenwell at April 25, 2007 12:53 PMtrace(StringUtils.beginsWith(price, "$"));
URL:
Thanks Ryan.
Posted by: Randy Troppmann at April 25, 2007 03:22 PMURL: http://www.spintechnologies.ca/flashblog
@Ray: Fixed that and updated the zip, lemme know if you find others.
Posted by: Ryan Matsikas at April 25, 2007 04:08 PM@Randy: No Problemo, hope you can put them to good use.
URL: http://gskinner.com
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!
Posted by: Nate Chatellier at April 25, 2007 06:24 PMURL: http://blog.natejc.com
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.
Posted by: Phillip Kerman at April 25, 2007 07:16 PMURL:
@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!
Posted by: Ryan Matsikas at April 26, 2007 10:42 AMURL: http://gskinner.com
AKA the gString class. :)
Posted by: Randy Troppmann at April 30, 2007 03:20 PMURL: http://www.spintechnologies.ca/flashblog
I just downloaded the class and ran a compile. I got the following errors:
Warning: 3596: Duplicate variable definition. @ line 279
and
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!
Posted by: Jesse Freeman at May 2, 2007 08:48 AMURL: http://www.flashbum.com
awesome, thanks for sharing - lookin forward to the array and date utils!
Posted by: taber at May 13, 2007 01:54 AMURL:
I think you want to revise StringUtils.endsWith():
p_string.indexOf(p_end) ...
to:
p_string.lastIndexOf(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!
Posted by: alinear at June 11, 2007 08:54 PMURL: http://www.alinear.net
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.
Posted by: dzedward at January 7, 2008 10:04 AMURL: http://www.flashgods.org/
@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.
Thanks!
Posted by: Lanny McNie at January 7, 2008 10:15 AMURL: http://gskinner.com
Thanks, that sounds great. Yea, they can get annoying hehe
Posted by: dzedward at January 8, 2008 09:20 AMURL: http://www.flashgods.org/
What if I want to find a word in a string?
Posted by: theranch at March 3, 2008 02:37 PMURL: http://www.thespikeranch.com
i tried to use this extension but recently found one i like better on peakstudios.com it creates the code for you and uploads all of the classes on instalation.
Posted by: Jeremy at April 21, 2008 10:00 AMURL: http://www.skitripusa.com
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.
Posted by: Dan at June 16, 2008 10:52 AMURL: http://www.danwashere.com
thanks alot !! It's very what I want!!! ^^
Posted by: hyunho lee at November 10, 2008 07:53 PMHave a good day~!
URL:
thanks. great work!
Posted by: koala at November 13, 2008 07:47 AMURL:
Thanks a lot for the useful extension. It works perfectly!
Posted by: Luca at January 30, 2009 08:04 AMURL:
Thank-you for releasing these functions!
One thing you need to do: add "|\|" to the end of the regex in escapePattern. Thanks again.
Posted by: Dallan Quass at February 7, 2009 02:30 PMURL: http://www.werelate.org/wiki/User:Dallan
This is a great tool, thanks!
Posted by: lars // dk at February 13, 2009 07:42 AMURL:
Very useful tools, thank you for hard work!
Posted by: en3rgizer at March 18, 2009 04:23 AMURL: http://en3rgizer.blogspot.com
It works like a charm!
Posted by: Jk_ at April 28, 2009 02:26 AMURL:
Great, thanks for these rocking Utilz.
I did find a small typo with 'removeExtraWhiteSpace', it should be 'removeExtraWhitespace'. To many humps.
Posted by: Dano Manion at July 8, 2009 01:54 PMURL: http://www.danomanion.com
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).
Posted by: Victor at August 14, 2009 04:20 PMI 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!
URL:
Hi,
is it possible to find the index(startIndex & endIndex) of specified word from the input text box... for example
Posted by: PruthviRaj at September 1, 2009 06:49 AMinput 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....
URL:
Thanks for this v.handy.
Posted by: Josh Noble at September 3, 2009 07:56 AMURL: http://www.visualdialects.co.uk
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?
Posted by: lauritz at September 25, 2009 11:50 AMURL:
Thanks for this free actionscript resource. Really helps alot.
Posted by: Newbie at September 28, 2009 08:36 AMURL: http://portaltoafrica.com
Awesome class!
@Victor
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:
http://pastebin.com/f175fd99d
public static function replace(p_string:String, p_remove:String,p_replace:String = '', p_caseSensitive:Boolean = true):String {
Posted by: Charlie Schulze at December 16, 2009 07:23 PMif (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);
}
URL: http://www.papervision2.com
This is Brilliant, Many thanks!
Posted by: Philip at December 28, 2009 11:32 AMURL: http://www.how-to-code.com
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?
Posted by: Josh at January 9, 2010 08:57 AMURL: http://www.iamsecond.com
"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.
Cheers,
Adrian
Posted by: Adrian Parr at February 7, 2010 04:03 AMURL: http://www.adrianparr.com
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
Cheers,
ASM
Posted by: ASM at February 7, 2010 08:08 PMURL: