ExtString class from mx.community.com
The ExtString class extends the ActionScript String class and contains one method only. It’s a really sweet little method for finding and replacing all occurences of a String within an ExtString.
Here’s the class, in all it’s simplicity.
class com.communitymx.Utils.ExtString extends String { function ExtString(str){ super(str); } public function findReplace(sFind:String, sReplace:String):String { return this.split( sFind ).join( sReplace); } }
Very nice find and replace: no looping, no storing indices. Here’s how that method works:
The following code stretches out the steps, using some plain old Strings and an Array. Makes the process more explicit. (Paste it into a new Flash file and watch it work.)
var mySentence:String = "This is my word in my sentence." ; trace( "mySentence is " + mySentence ) ; var textHolder:Array = mySentence.split( "my" ) ; trace( "After splitting mySentence using 'my' as a separator, ") trace( "the method split() returns an array. ") trace( "We put it in an array called textHolder, that looks like this:" ); // check out what's in our array: for( var i = 0 ; i < textHolder.length ; i++ ){ trace( "textHolder[" + i + "] is " + textHolder[i] ); } trace( "Now we'll use the Array method join() to glue the string back together." ) ; trace( "The method join() takes a parameter that it uses as a separator too, " ); trace( "but it sticks it in instead of chopping it out." ); trace( "If we use 'you' as the separator, the join() method will insert 'you' "); trace( "between each of the array elements in textHolder."); trace( "That gives us a new sentence, a string returned by the join method." ) ; var myNewSentence:String = textHolder.join( "your" ); trace( "myNewSentence is " + myNewSentence ) ;
Lovely, no? Thanks to communitymx.com