scala 如何将句子拆分为多个空格分隔的单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14469958/
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:14  来源:igfitidea点击:

How to split sentence into words separated by multiple spaces?

scala

提问by yalkris

The following code:

以下代码:

val sentence = "1 2  3   4".split(" ")

gives me:

给我:

Array(1, 2, "", 3, "", "", 4)

but I'd rather want to have only the words:

但我宁愿只拥有这些词:

Array(1, 2, 3, 4)

How can I split the sentence when the words are separated by multiple spaces?

当单词被多个空格分隔时,如何拆分句子?

回答by Tim

Use a regular expression:

使用正则表达式:

scala> "1   2 3".split(" +")
res1: Array[String] = Array(1, 2, 3)

The "+" means "one or more of the previous" (previous being a space).

“+”表示“前一个或多个”(前一个是空格)。

Better yet, if you want to split on all whitespace:

更好的是,如果您想拆分所有空白:

scala> "1   2 3".split("\s+")
res2: Array[String] = Array(1, 2, 3)

(Where "\\s"is a Patternwhich matches any whitespace. Look herefor more examples.)

(匹配任何空格"\\s"Pattern在哪里。查看此处以获取更多示例。)

回答by Brian

You can filter out the ""from the split Array.

您可以""从 split 中过滤掉Array

scala> val sentence = "1 2  3   4".split(" ").filterNot(_ == "")
sentence: Array[java.lang.String] = Array(1, 2, 3, 4)

回答by elm

This regular expression \\W+delivers (alphaunmerical) words, thus

此正则表达式\\W+提供(字母数字)单词,因此

val sentence = "1 2  3   4".split("\W+")
sentence: Array[String] = Array(1, 2, 3, 4)

For ease of use, in Scala 2.10.* and 2.11.* consider

为了便于使用,在 Scala 2.10.* 和 2.11.* 中考虑

implicit class RichString(val s: String) extends AnyVal {
  def words = s.split("\W+")
}

Thus,

因此,

sentence.words
res: Array[String] = Array(1, 2, 3, 4)