Find common words in 2 strings
Show older comments
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'
Accepted Answer
More Answers (1)
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
1 Comment
Elysi Cochin
on 3 May 2016
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!