common_word = intersect(strsplit(string1),strsplit(string2))
Run Code Online (Sandbox Code Playgroud)
strsplit将每个字符串拆分为单词单元,然后intersect找出共同的字符串。
如果您想避免strsplit使用,可以regexp改用-
common_word =intersect(regexp(string1,'\s','Split'),regexp(string2,'\s','Split'))
Run Code Online (Sandbox Code Playgroud)
奖励:从常用词中删除停用词
让我们添加stop-words这两个字符串的共同点-
string1 = 'hello my name is bob and I am going to the zoo'
string2 = 'today bob went to the park'
Run Code Online (Sandbox Code Playgroud)
使用前面介绍的解决方案,您将获得-
common_word =
'bob' 'the' 'to'
Run Code Online (Sandbox Code Playgroud)
现在,这些词- 'the'和'to'是停用词的一部分。如果你想有其删除,让我提出这一点- Removing stop words from single string
和它的接受的解决方案。
最终的输出将是'bob',您正在寻找谁!