17 July 2011

AS3: Simple, Powerful String Find & Replace all instances function for Flash Actionscript. Similar to PHP's str_replace replacement.

Exactly what it says on the tin!

AS2 version here



Please see right hand pane before copying my code - thanks! >>

//needle and/or replacement can be either a string or an array of strings:
function replace ( needle, replacement, haystack:String):String{
   var r=haystack;
   if(typeof(needle)=="string"){
    needle = [needle]
   }
   if(typeof(replacement)=="string"){
    replacement = [replacement]
   }
   while(replacement.length<needle.length){
    replacement.push(replacement[replacement.length-1])
   }
   for(var i in needle){
    r = r.split(needle[i]).join(replacement[i])
   }
   return(r)
  }
//SAMPLE USAGE:
trace( replace( "A", "X", "ABCDEFG" ) )  //outputs: XBCDEF
trace( replace(["A","B","C"] , ["X","Y","Z"], "ABCDEFG") )  //outputs: XYZDEF

5 comments:

JannethManalu said...
This comment has been removed by the author.
Susan said...

192.168.l.254.254 IP address is allocated by many brands to log in to the router management interface.

Anonymous said...

address 7a replica bags wholesale original site dolabuy louis vuitton over at this website replica louis vuitton bags

Unknown said...

v1n37e8r95 q8i58e5w92 q7x46m4i78 x4e45g1e67 l2t03n0z18 i2m93j8y39

Anonymous said...

penis

Post a Comment

Cron Design Studio: Dublin based web design & software development