1

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! :)

4 Answers 4

1

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 " ".

Sign up to request clarification or add additional context in comments.

1 Comment

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

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 ", "

Comments

0

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.

Comments

0

@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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.