Michael Michael - 22 days ago 8
Scala Question

How to find the longest common prefix of two strings in Scala?

How to find the longest common prefix of two strings in Scala?

I probably can code an "imperative" solution (with an index

i
running over the strings while
s(i) == t(i)
) but I am looking for a "functional-style" solution (without updating the index variable explicitly, for instance).

Answer
scala> "helloworld".zip("hellohell").takeWhile(Function.tupled(_ == _)).map(_._1).mkString
res130: String = hello