在scala中查找两个字符串之间的多个重叠?

时间:2017-09-19 10:54:39

标签: string scala overlap

假设我有以下字符串:

my_str1 = "Some Text is here too"
my_str2 = "Text was present here too"

因此重叠的字词为Textheretoo

我看到了this question here并想知道是否有可能将其扩展到我的多个重叠问题?

编辑:字符串也可以是连续的。像这样:

my_str1 = "SomeTextisheretoo"
my_str2 = "Textwaspresentheretoo"

因此,在这种情况下,输出将为Textheretoo

1 个答案:

答案 0 :(得分:2)

对于您的方案,str在字词之间有空格,您只需使用intersect即可获得重叠字词,例如:

val res1 = "Some Text is here too".split("\\s+")
val res2 = "Text was present here too".split("\\s+")

res1.intersect(res2)

> res: Array[String] = Array(Text, here, too)

Doc:https://www.scala-lang.org/api/current/scala/Array.html#intersect(that:Seq[A]):Array[A]

相关问题