How I would determine if a string contains multiple substrings?

조회 수: 239 (최근 30일)
Pat Canny
Pat Canny 2023년 2월 23일
댓글: Steven Lord 2023년 11월 16일
I'd like to know if a string has multiple substrings (e.g, words) in it.
For example:
myString = "This has some words in it.";
I would want to know if that string has "This" AND "some". I know I can use "contains" to look for individual substrings, but I want to know if ALL substrings are in a string.

채택된 답변

Pat Canny
Pat Canny 2023년 2월 23일
편집: Pat Canny 2023년 8월 4일
Here is one way to do this using arrayfun:
myString = "This has some words in it.";
subStrings = ["This","some"];
hasAllSubstrings = all(arrayfun(@(substr) contains(myString, substr, 'IgnoreCase', true), subStrings))
  댓글 수: 3
Pat Canny
Pat Canny 2023년 11월 11일
I think pattern may help you. Below is a simple example, but you can get fairly fancy with it.
testString1 = "ARRAY[";
testString2 = "ARRAY[something]";
testString3 = "ARRAY[]";
patternToMatch = "ARRAY[" + lettersPattern(3,10) + "]";
stringArray = [testString1;testString2;testString3];
isMissingPattern = arrayfun(@(str) ~contains(str, patternToMatch), stringArray)
isMissingPattern = 3×1 logical array
1 0 1
Ludo Houben
Ludo Houben 2023년 11월 16일
Hi @Pat Canny, thanks for your reply. I'm going to 'play' with this. Thank you.

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

추가 답변 (3개)

Bruno Luong
Bruno Luong 2023년 8월 4일
편집: Bruno Luong 2023년 8월 4일
myString = "This has some words in it.";
subStrings = ["This","some"];
hasAllSubstrings = isequal(regexp(myString, subStrings, 'once', 'match'), subStrings)
hasAllSubstrings = logical
1

Les Beckham
Les Beckham 2023년 2월 23일
편집: Les Beckham 2023년 2월 23일
myString = "This has some words in it.";
if contains(myString, "This") && contains(myString, "some")
disp Yes
else
disp No
end
Yes
  댓글 수: 1
Pat Canny
Pat Canny 2023년 2월 23일
Yep, I also tried that. It works fine for two substrings, but what if there are several?

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


Steven Lord
Steven Lord 2023년 11월 10일
myString = "This has some words in it.";
s = split(myString)
s = 6×1 string array
"This" "has" "some" "words" "in" "it."
whichWordsAreIn = ismember(["some"; "in"], s)
whichWordsAreIn = 2×1 logical array
1 1
allWordsIn = all(whichWordsAreIn)
allWordsIn = logical
1
You can combine this into one command if you want; I showed temporary variables to illustrate each step. For exclusions you want to wrap the ismember call in ~any() instead of all.
  댓글 수: 2
Stephen23
Stephen23 2023년 11월 16일
편집: Stephen23 2023년 11월 16일
myString = "This has some words in it.";
s = split(myString)
s = 6×1 string array
"This" "has" "some" "words" "in" "it."
whichWordsAreIn = ismember(["some"; "it"], s) % what happend to "it" ?
whichWordsAreIn = 2×1 logical array
1 0
Steven Lord
Steven Lord 2023년 11월 16일
I neglected to remove punctuation from the string before splitting it. The isstrprop function could help with this (though you'd have to be careful with punctuation included as part of a word, like "you'd" as I wrote earlier in this phrase.)
The accepted answer has a similar problem, if you change the text to be examined to include a word that contains as part of it one of the substrings being searched for.
myString = "This has something words in it.";
subStrings = ["This","some"]; % myString has "something", does that match "some"?
hasAllSubstrings = all(arrayfun(@(substr) contains(myString, substr, 'IgnoreCase', true), subStrings))
hasAllSubstrings = logical
1
For more general cases and processing lots of data, you may want to use the functionality for analyzing text data provided in Text Analytics Toolbox.

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

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by