필터 지우기
필터 지우기

Find common words in 2 strings

조회 수: 15 (최근 30일)
Elysi Cochin
Elysi Cochin 2016년 5월 3일
편집: Stephen23 2016년 5월 3일
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
  댓글 수: 1
Elysi Cochin
Elysi Cochin 2016년 5월 3일
thank you so much all of you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by