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 =patternMatching: 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 =patternMatching: digitsPattern + lettersPattern(1)
TF = contains(str,pat); str(TF)
ans = "221B Baker St."
For a list of functions that create pattern objects, seepattern
.
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
,忽略的情况。
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
字符串数组|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
字符串数组|character vector|cell array of character vectors|pattern
array (since R2020b)
Search pattern, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array(since R2020b)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
使用笔记and limitations:
str
andpat
must be a string scalar, character vector, or cell array containing not more than one character vector.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
使用笔记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).
See Also
count
|endsWith
|extract
|matches
|replace
|startsWith
|split
|pattern
|digitsPattern
|lettersPattern
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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
- 比利时(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(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)