...

View Full Version : String.replace() (php syntax)



itsallkizza
08-28-2009, 05:29 PM
AS2 doesn't have a replace function built into the String class, but this is the quickest solution I came up with. It doesn't accept regular expressions, just simple strings. I'm posting this to get feedback from scripting pros who like to optimize the hell out of things (as do I, I'm just being lazy atm); this method is using the String.split and Array.join which can be somewhat resource consuming so feel free to criticize/offer better solutions. Also, I used a functional solution rather than modifying the String's prototype for ease of understanding for newbies. A *.as (or root frame 1) prototype solution could likely perform more speedily.

str_replace():


function str_replace(search:String,replace:String,subject:String):String
{
return subject.split(search).join(replace);
}


example:


var my_str:String = "Old Billy Bob Jones went down to the old store to buy some old cigarettes.";
trace(str_replace("old","new",my_str));



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum