contains
Determine if pattern is in strings
Description
Examples
Find Text
Create a string array of names, where some names containPaul
.
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 thestring
function instead of double quotes.
Return a logical array where the position of each element equal to1
corresponds to the position of a string instr
that containsPaul
.
pat ="Paul"; TF = contains(str,pat)
TF =1x3 logical array0 1 1
Display the strings that containPaul
. Index back intostr
usingTF
.
str(TF)
ans =1x2 string"Paul Jay Burns" "John Paul Smith"
Search Using Patterns
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 thedigitsPattern
function.
pat = digitsPattern
pat =模式Matching: digitsPattern
Return a logical array indicating which strings contain digits. Display the matching strings.
TF = contains(str,pat)
TF =1x3 logical array1 0 1
str(TF)
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 =模式Matching: digitsPattern + lettersPattern(1)
TF = contains(str,pat); str(TF)
ans = "221B Baker St."
For a list of functions that create pattern objects, see模式
.
Match Any Text in List
Create a string array of names, where some names contain eitherAnn
orPaul
.
str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]
str =1x3 string"Mary Ann Jones" "Christopher Matth..." "John Paul Smith"
Find the elements ofstr
that contain eitherAnn
orPaul
.
pat = ["Ann","Paul"]; TF = contains(str,pat)
TF =1x3 logical array1 0 1
Index back intostr
usingTF
.
str(TF)
ans =1x2 string"Mary Ann Jones" "John Paul Smith"
Ignore Case
Create a string array that contains names. Determine which names containanne
, ignoring case.
Starting in R2017a, you can create strings using double quotes.
str = ["Anne","Elizabeth","Marianne","Tracy"]
str =1x4 string"Anne" "Elizabeth" "Marianne" "Tracy"
pat ="anne"; TF = contains(str,pat,'IgnoreCase',真正的)
TF =1x4 logical array1 0 1 0
Display the strings that containanne
. Index back intostr
usingTF
.
str(TF)
ans =1x2 string"Anne" "Marianne"
Determine If Character Vector Contains Substrings
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 =logical1
TF = contains(chr,'pineapples')
TF =logical0
Input Arguments
str
—Input text
string array|character vector|cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
pat
—Search pattern
string array|character vector|cell array of character vectors|模式
array (since R2020b)
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
模式
array(since R2020b)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
Pattern objects are not supported.
For more information, seeTall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
andpat
must be a string scalar, character vector, or cell array containing not more than one character vector.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
pat
must be a string array, character vector, or a cell array of character vectors.
For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Open Example
A modified version of this example exists on your system. Do you want to open this version instead?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- 西班牙(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)