I need some help in adding the replace all functionality into my Flex project. I would prefer doing this in a way that is as natural as possible.
What I want to achieve is to be able to run this code (with the Flex compiler) "aabbaaba".replaceAll("b","c")
and get "aaccaaca"
. Also I want to chain replaceAll
calls.
Note: I won't be actually replacing b
s with c
s, but various string that will not be known at coding time!
What I don't want:
1. Use regular expressions with the global flag. The tokens to replace are determined ad run time and transforming them into a regular expression isn't straight forward.
2. Use the StringUtil.replace
method. It's a static method and chaining is ugly.
3. Chain split
and join
. Because it may be confusing for others when reading the code.
4. Disable strict type checking. I want to have type checking for the rest of my code.
Here is what I have so far:
String.prototype.replaceAll = function(replace:String, replaceWith:String):String{
return this.split(replace).join(replaceWith);
}
And can be called like this:
"aababacaaccb"["replaceAll"]("b", "c")["replaceAll"]("c", "a");
Also, are there any recommendations against extending objects through the prototype? I will also accept an answer that has strong arguments against extending the String object through the prototype.
Thank you,
Alin
Why not? for simple string replacements is quite straight forward and performant:
or if you prefer:
I wouldn't recommend using prototype for this, it's not very OOP nor standard... it feels too hacky for such a simple operation.
I think you got all the technical answers possible. I will elaborate in what I think is the best way to approach this language-wise.
Prototypes are not recommended in an OOP language such as AS3 (mostly because they defy encapsulation). You imply that you don't want something "confusing to others" (in relation to split.join); well, prototypes in AS3 are very confusing. Just as an example of this, a prototype declaration can be done from anywhere in your code, and as such is not obvious where it should reside. If "others" encounter "foo".replaceAll() in your code, it's not at all obvious where one can find that method and check what it really does.
A static function amends this and is pretty straight forward. Sure, you need an extra argument, and you can't really chain properly, but is this such a bad thing?
If you need performance, split.join is the way to go. I would bet good money that more AS3 devs know about split.join than the use of prototypes.
On the other hand, I think the most semantic and pragmatic way would be to use the own language method (hence my previous answer). You are trying to replace all needles in a string with another string in AS3, and for that the language has the String::replace method with the global flag. I'm pretty sure there is a way to easily parse and use any string in regexp.
I concur that in some cases a helper method (like replaceAll) could be necessary, but I would strongly encourage you to not use prototypes, and instead use a more standard way, like StringUtil.replace.
I can't think of a way to meet the 4 requirements you posted. But I think if that your main goal is replacing various tokens in one go (what you wanted to achieve with chaining calls) while being able to use any string as a token, you can try something like this:
There's one caveat to keep in mind. With an Object object, the order is not guaranteed. In most cases, this isn't important, but if in your case it is, you could use an Array instead, and in each slot store both the token and its replacement (as an Object, for instance).
I know this is an old thread, but this is what I came up with which can do any regular string.
"aabbaaba".split('b').join('c')
returns 'aaccaaca'UPD:
i wonder why it's unacceptable for you (however it doesn't support regexp): it works pretty fast (checked it in severe loops) and does exactly what you want.
and also there's a couple of solutions that's not mentioned in your blacklist (btw static function is better then messing with prototype - imho):
replaceAll
method andsuper()
call. instances of this class will returntrue
if you check(myStringInstance is String)
The second answer is this:
write the wrapper class StringEx for String and you can define replaceAll to be chained like this
by providing toString() you can easily change StringEx object into String
you can get full-version here: OOP way of prototype extension - wonderfl build flash online