Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts

    String.replace() (php syntax)

    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():
    Code:
    function str_replace(search:String,replace:String,subject:String):String
    	{
    	return subject.split(search).join(replace);
    	}
    example:
    Code:
    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));
    Last edited by itsallkizza; 08-28-2009 at 05:45 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •