Bha*_*waj 3 scala scala-collections
我有一个很大的字符串序列,我只对找到某个字符串后的部分感兴趣.例如,序列可以是 -
..
..
one
two
three
four
five
..
..
Run Code Online (Sandbox Code Playgroud)
我希望过滤掉四个之前的所有行,以便只包含一个过滤序列(四个,五个......等等......)
如何以功能方式在Scala中编写它?
提前致谢.
它是存储在文件中还是存储在某种集合中?
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)
| 归档时间: |
|
| 查看次数: |
621 次 |
| 最近记录: |