Find common words in 2 strings

13 次查看(过去 30 天)
i have 2 string
str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under tree');
In these 2 strings 'rabbit' 'is' 'tree' is common.... How can i find the common words present in 2 strings....
i want result
str = 'rabbit' 'is' 'tree'

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-5-3
编辑:Azzi Abdelmalek 2016-5-3
str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under tree');
str=intersect(strsplit(str1),strsplit(str2))
OR, to maintain the order
str=intersect(strsplit(str1),strsplit(str2),'stable')

更多回答(1 个)

Stephen23
Stephen23 2016-5-3
编辑:Stephen23 2016-5-3
This works in MATLAB versions prior to R2013a:
>> str1 = ('rabbit is eating grass near a tree');
>> str2 = ('rabbit is sleeping under tree');
>> w1 = regexp(str1,'\S+','match');
>> w2 = regexp(str2,'\S+','match');
>> intersect(w1,w2)
ans =
is
rabbit
tree

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by