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.

Download ActionScript file

Share and bookmark:

  • Twitter
  • Technorati
  • StumbleUpon
  • Digg
  • Google Bookmarks
  • Facebook
  • MySpace
  • del.icio.us
  • Mixx
  • blogmarks
  • Live
  • NewsVine
  • Reddit
  • Slashdot
  • LinkedIn
  • FriendFeed
  • email