如何在Scala中删除数字?
例如我有这样的文字:
canon 40 22mm lens lock strength plenty orientation 321 .
删除后:
canon lens lock strength plenty orientation .
答案 0 :(得分:7)
请尝试filter
或filterNot
val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filter(!_.isDigit)
或
val text = "canon 40 22mm lens lock strength plenty orientation 321 ."
val without_digits = text.filterNot(_.isDigit)
答案 1 :(得分:1)
答案 2 :(得分:1)
因为很明显,你想要删除包含数字的所有单词,因为在你的例子中mm
也消失了,因为它以数字作为前缀。
val s = "That's 22m, which is gr8."
s.split(" ").filterNot(_.exists(_.isDigit)).mkString(" ")
res8: String = That's which is