scala 从字符串中删除多种字符类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14495613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:52:26  来源:igfitidea点击:

Removing multiple character types from a string

scala

提问by Hyman

Is this an acceptable approach for removing multiple character types from a string or is there a better (more efficient way)? The "ilr".contains(_)bit feels a little like cheating considering it will be done for each and every character, but then again, maybe this is the right way. Is there a faster or more efficient way to do this?

这是从字符串中删除多个字符类型的可接受方法还是有更好(更有效的方法)?该"ilr".contains(_)位感觉就像作弊考虑它会为每一个字符做了一些,但话又说回来,也许这是正确的方式。有没有更快或更有效的方法来做到这一点?

val sentence = "Twinkle twinkle little star, oh I wander what you are"

val words = sentence.filter(!"ilr".contains(_))   

// Result: "Twnke twnke tte sta, oh I wande what you ae"

回答by

I'd just use Java's good old replaceAll(it takes a regexp):

我只是使用 Java 的旧版本replaceAll(它需要一个正则表达式):

"Twinkle twinkle little star, oh I wander what you are" replaceAll ("[ilr]", "")
// res0: String = Twnke twnke tte sta, oh I wande what you ae

In contrast to working with chars (as in filtering a Seq[Char]), using regular expressions should be Unicode-safe even if you're working with code points outside the basic multilingual plane. "There Ain't No Such Thing As Plain Text."

与使用chars (如过滤 a Seq[Char])相比,使用正则表达式应该是 Unicode 安全的,即使您使用的是基本多语言平面之外的代码点。“没有纯文本这样的东西。”

回答by om-nom-nom

There would be no significant difference, since there is only 3 characters to remove and no so big string to filter, but you may consider to use Set for this purpose. E.g.

不会有显着差异,因为只有 3 个字符要删除,也没有那么大的字符串要过滤,但您可以考虑使用 Set 来实现此目的。例如

val toRemove = "ilr".toSet
val words = sentence.filterNot(toRemove)