String.replace() method for ActionScript2
Posted on 23rd November 2007 in ActionScript |
Another snippet for free — String.replace() prototype for Flash that can be used to replace a pattern in a String.
The code of the String.replace() prototype is very simple and somewhat similar to the PHP’s str_replace function:
String.prototype.replace = function(searchStr, replaceStr):String { var arr:Array = this.split(searchStr); return arr.join(replaceStr); };
Example:
// initial string with a placeholder var str:String = '$person is welcome to flash tekkie'; // replace $person with 'Flash developer' and trace it var replacedStr:String = str.replace('$person','Flash developer'); trace(replacedStr);
As seen above method takes 2 arguments. Firstly a string pattern to search for and secondly a string to replace the pattern(s) found. Method returns a String.

4 Responses
Thanks for the script. It was useful for me
Thank you very much for the script.I was looking for something like that
or even simpler:
return this.split(searchStr).join(replaceStr);
Yes, why not.