过滤字符串序列直到在scala中找到密钥的功能方法

Bha*_*waj 3 scala scala-collections

我有一个很大的字符串序列,我只对找到某个字符串的部分感兴趣.例如,序列可以是 -

..
..
one
two
three
four
five
..
..
Run Code Online (Sandbox Code Playgroud)

我希望过滤掉四个之前的所有行,以便只包含一个过滤序列(四个,五个......等等......)

如何以功能方式在Scala中编写它?

提前致谢.

sen*_*nia 9

它是存储在文件中还是存储在某种集合中?

dropWhile所有scala集合中都有方法:

val s = Seq("..", "..", "one", "two", "three", "four", "five", "..", "..")
// Seq[String] = List(.., .., one, two, three, four, five, .., ..)

s.dropWhile{ _ != "four" }
// Seq[String] = List(four, five, .., ..)
Run Code Online (Sandbox Code Playgroud)

它适用于Iterator,所以你可以像这样使用它:

val lines = io.Source.fromFile("bigFile.txt").getLines().dropWhile{ _ != "four" }
Run Code Online (Sandbox Code Playgroud)