0

I primarily work with AS3 when dealing with flash, but I have a need to use AS2 for a particular project, AS2 is not my friend.

I need to replace a series of substrings within some data on a regular basis. Normally I would create a cleaning function that utilised string.replace() and run my data through it.

However the string.replace() function is missing from AS2 and I haven't found an equivalent. What would be the simplest method for achieving similar functionality?

example:

dirtydata = "I have ABCtoast withABCABC jamABC"

my result would be:

cleandata = "I have toast with jam" 
0

1 Answer 1

2

Do this,

String.prototype.replace = function(searchStr, replaceStr):String { 
    var arr:Array = this.split(searchStr);
    return arr.join(replaceStr);
};  

// initial string with a placeholder
var str:String = 'I have ABCtoast withABCABC jamABC';
// replace ABC with '' and trace it
var replacedStr:String = str.replace('ABC','');
trace(replacedStr)

Else, you could also go for a function performing split and join on the same line.

function stringReplace(block:String, find:String, replace:String):String
{
return str.split(searchStr).join(replaceStr);
}
Sign up to request clarification or add additional context in comments.

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.