If I have a List of type String,
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)
how can I split it into n
separate lists based on a certain string/pattern ("BREAK"
, in this case).
I've thought about finding the position of "BREAK"
with indexOf
, and split up the list that way, or using a similar approach with takeWhile (i => i != "BREAK")
but I'm wondering if there's a better way?
If it helps, I know there will only ever be 3 sets of items in the items
list (thus 2 "BREAK"
markers).