Determine if pattern is in strings
TF = contains(str,pat)
TF = contains(str,pat,'IgnoreCase',true)
example
TF = contains(str,pat) returns 1 (true) if str contains the specified pattern, and returns 0 (false) otherwise.
str
pat
1
true
0
false
If pat is an array containing multiple patterns, then contains returns 1 if it finds any element of pat in str.
contains
TF = contains(str,pat,'IgnoreCase',true) ignores case when determining if str contains pat.
collapse all
Create a string array of names, where some names contain Paul.
Paul
Starting in R2017a, you can create strings using double quotes.
str = ["Mary Ann Jones","Paul Jay Burns","John Paul Smith"]
str = 1x3 string "Mary Ann Jones" "Paul Jay Burns" "John Paul Smith"
If you are using R2016b, create string arrays using the string function instead of double quotes.
string
Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that contains Paul.
pat = "Paul"; TF = contains(str,pat)
TF = 1x3 logical array 0 1 1
Display the strings that contain Paul. Index back into str using TF.
TF
str(TF)
ans = 1x2 string "Paul Jay Burns" "John Paul Smith"
Since R2020b
Create a string array that contains addresses.
str = ["221B Baker St.","Tour Eiffel Champ de Mars","4059 Mt Lee Dr."]
str = 1x3 string "221B Baker St." "Tour Eiffel Champ..." "4059 Mt Lee Dr."
To find addresses that contain numbers, create a pattern that matches an arbitrary number of digits by using the digitsPattern function.
digitsPattern
pat = digitsPattern
pat = pattern Matching: digitsPattern
Return a logical array indicating which strings contain digits. Display the matching strings.
TF = 1x3 logical array 1 0 1
ans = 1x2 string "221B Baker St." "4059 Mt Lee Dr."
Search for strings that have a sequence of digits followed by one letter. You can build more complex patterns by combining simple patterns.
pat = digitsPattern + lettersPattern(1)
pat = pattern Matching: digitsPattern + lettersPattern(1)
TF = contains(str,pat); str(TF)
ans = "221B Baker St."
For a list of functions that create pattern objects, see pattern.
pattern
Create a string array of names, where some names contain either Ann or Paul.
Ann
str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]
str = 1x3 string "Mary Ann Jones" "Christopher Matth..." "John Paul Smith"
Find the elements of str that contain either Ann or Paul.
pat = ["Ann","Paul"]; TF = contains(str,pat)
Index back into str using TF.
ans = 1x2 string "Mary Ann Jones" "John Paul Smith"
Create a string array that contains names. Determine which names contain anne, ignoring case.
anne
str = ["Anne","Elizabeth","Marianne","Tracy"]
str = 1x4 string "Anne" "Elizabeth" "Marianne" "Tracy"
pat = "anne"; TF = contains(str,pat,'IgnoreCase',true)
TF = 1x4 logical array 1 0 1 0
Display the strings that contain anne. Index back into str using TF.
ans = 1x2 string "Anne" "Marianne"
Create a character vector that contains a list of foods. Determine if the names of different foods are in the character vector.
chr = 'peppers, onions, and mushrooms'; TF = contains(chr,'onion')
TF = logical 1
TF = contains(chr,'pineapples')
TF = logical 0
Input text, specified as a string array, character vector, or cell array of character vectors.
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern array (since R2020b)
Usage notes and limitations:
Pattern objects are not supported.
For more information, see Tall Arrays.
str and pat must be a string scalar, character vector, or cell array containing not more than one character vector.
pat must be a string array, character vector, or a cell array of character vectors.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
count | digitsPattern | endsWith | extract | lettersPattern | matches | pattern | replace | split | startsWith
count
endsWith
extract
lettersPattern
matches
replace
split
startsWith
You have a modified version of this example. Do you want to open this example with your edits?