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 (18)
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