I would like to know how I can split a string using more than one delimiter with Scala.
For instance if I have a list of delimiters :
List("Car", "Red", "Boo", "Foo")
And a string to harvest :
Car foerjfpoekrfopekf Red ezokdpzkdpoedkzopke dekpzodk Foo azdkpodkzed
I would like to be able to output something like :
List( ("Car", " foerjfpoekrfopekf "),
("Red", " ezokdpzkdpoedkzopke dekpzodk "),
("Foo", " azdkpodkzed")
)
a bit verbose, but it works:
DEPRECATED VERSION: (it has a bug, left it here because you already accepted the answer)
output:
it returns
Array[String]instead of list. but you can just as well do:f(s, l, g).toListEDIT:
just noticed this code is good if the delimiters only appear once in the string. if had defined
sas follows:I’d still get the same result, instead of another pair
("Car"," more...")EDIT#2: BUGLESS VERSION here’s the fixed snippet:
output:
and now it works 🙂