views:

2223

answers:

5

I have a function that works great in Actionscript 2, except it can only replace one character. I need it to behave more like str_replace in PHP and replace an array of characters.

Here is the code I have now, it simply replaces a space ( ) with a hyphen (-) in a String.

function str_replace(str){
    return str.split(" ").join("-");
}

What I am trying to do is replace spaces, commas, and combinations of characters (ex. space and comma) from Actionscript strings for use in URLs.

So this:

Shop, Dine, Play

Will become this:

Shop-Dine-Play

Any suggestions are greatly appreciated! :)

A: 

Does this work for you?

function replace(txt:String, fnd:String, rep:String):String
{
    return txt.split(fnd).join(rep);
}

trace(replace("Shop, Dine, Play", ", ", "-"));//Shop-Dine-Play

i.e. the string you search for can contain more than one character, in this case ", "

tarling
A: 

If you want to replace an array of characters with another array of characters, you can do something like

function replace(str:String, toFind:Array, toReplace:Array):String
{
    if(toFind.length != toReplace.length)
        throw new Error("Error : Find and replace array must match in length");


    for(var i:Number = 0; i < toFind.length; i++)
    {
        str = str.split(toFind[i]).join(toReplace[i]);
    }

    return str;
}

And use it like this :

replace("abc", ["a", "b", "c"], ["c", "b", "a"]);   //result cba

Note that this is really not optimal if you want to replace alot of characters in a long string.

Subb
+1  A: 

For your case, the simplest way would be to do a sequence of your split/join commands in the order of longest to shortest replacement.

e.g.,

txt = txt.split(", ").join(-)

txt = txt.split(",").join(-)

txt = txt.split(" ").join(-)

So that you don't get Shop--Dine--Play, you replace ", " first, then "," or " ".

Brendan Dowling
this worked great! I just modified it slightly to work with my function:str = str.split(", ").join('-');
Marcy Sutton
A: 

could someone make an example of this in action? like being triggered by a click event and a dynamic txt string being passed in?

thanks!

ryan cole
as2 or as3? You can simply call the str_replace function, with your dynamic text as a parameter, inside of your onPress function in AS2 or click listener in AS3. I haven't actually tested the str_replace part in AS3, but it should work - example below. private function str_replace(str:String):String { return str.split(" ").join("-"); } private function clickListener(e:MouseEvent) { newString = str_replace('this is a string'); trace(newString); // outputs 'this-is-a-string'; } theTextField.addEventListener(MouseEvent.CLICK, clickListener);
Marcy Sutton
A: 

@Ryan -- my comment lost all the formatting, so here it is again. And I just realized it is the same str_replace function I originally provided. but it works!

as2 or as3? Either way, you can simply call the str_replace() function, with your dynamic text as a parameter, inside of your onPress() function in AS2 or click listener in AS3. I haven't actually tested the str_replace part in AS3, but it should work - example below.

private var newString:String; 

// elsewhere in your document
private function str_replace(str:String):String { 
  return str.split(" ").join("-"); 
} 
private function textClickListener(e:MouseEvent) { 
   if(e.target is TextField){ 
      newString = str_replace(e.target.text); 
      trace(newString); // outputs theTextField.text; 
   }
} 
theTextField.addEventListener(MouseEvent.CLICK, textClickListener); 
// this assumes you have a dynamic text field named 'theTextField'
Marcy Sutton