本文介绍了将replaceAll方法添加到ActionScript String类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来添加替换我的Flex项目的所有功能。我希望以尽可能自然的方式做到这一点。



我想实现的是能够运行此代码(使用Flex编译器)aabbaaba.replaceAll(b,c)并获得aaccaaca。另外我想连锁 replaceAll 调用。

注意:我不会实际替换 b s c s,但是在编码时不会知道的各种字符串!



我不想要的:

1.使用带有全局标志的正则表达式。要替换的令牌是确定的广告运行时间,并将它们转换为正则表达式不是直接的。

2.使用 StringUtil.replace 方法。这是一个静态的方法,链接是丑陋的。

3.链 split 和 join 。因为读取代码时可能会让其他人感到困惑。

4.禁用严格的类型检查。我想为我的代码的其余部分进行类型检查。




这是我到目前为止:

<$ p $ (替换:String,replaceWith:String):String {
返回this.split(替换).join(replaceWith);

$ / code>

可以这样调用:

 aababacaaccb[replaceAll](b,c)[replaceAll](c,a); 






原型? 我也接受一个答案,强烈反对通过原型扩展String对象。



谢谢,

alin

解决方案

我想你可以得到所有的技术答案。我将详细说明我认为是以这种语言进行处理的最佳方式。

不推荐在AS3等OOP语言中使用原型(主要是因为它们无法封装)。你意味着你不想要让别人困惑(与split.join有关);好吧,AS3中的原型非常混乱。
就这个例子来说,原型声明可以在你的代码中的任何地方完成,因此在它应该驻留的地方并不明显。如果其他人在你的代码中遇到foo.replaceAll(),那么在哪里可以找到这个方法并检查它的真实含义并不是一个明显的例子。

静态功能修改这是非常简单的。当然,你需要一个额外的论点,你不能真正链接正确,但是这是一个坏东西?



如果您需要性能,split.join是要走的路。我敢打赌,有更多的AS3开发人员知道split.join比使用原型更好。

另一方面,我认为语义和实用性最强的方法是使用自己的语言方法(因此我以前的答案)。您正试图用AS3中的另一个字符串替换一个字符串中的所有针,并且为此,该语言具有带有全局标志的String :: replace方法。我很肯定有一种方法可以轻松地解析和使用正则表达式中的任何字符串。

我同意在某些情况下可能需要一个辅助方法(如replaceAll) ,但我强烈建议你不要使用原型,而应使用更为标准的方式,比如StringUtil.replace。


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 bs with cs, 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

解决方案

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.

这篇关于将replaceAll方法添加到ActionScript String类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:56