Scala按分隔符拆分列表的惯用方法是什么?
如果我有一个字符串类型的列表,
scala> val items = List("Apple","Banana","Orange","Tomato","Grapes","BREAK","Salt","Pepper","BREAK","Fish","Chicken","Beef")
items: List[java.lang.String] = List(Apple, Banana, Orange, Tomato, Grapes, BREAK, Salt, Pepper, BREAK, Fish, Chicken, Beef)
我如何根据某个字符串/模式将其拆分为单独的列表(在这种情况下)。n
"BREAK"
我曾想过找到 with 的位置,并以这种方式拆分列表,或者使用类似的方法,但我想知道是否有更好的方法?"BREAK"
indexOf
takeWhile (i => i != "BREAK")
如果它有帮助,我知道列表中只有3组项目(因此2个标记)。items
"BREAK"