contains
确定字符串中是否有模式
说明
示例
查找文本
创建一个由人名组成的字符串数组,其中一些人名包含 Paul
。
您可以使用双引号创建字符串。
str = ["Mary Ann Jones","Paul Jay Burns","John Paul Smith"]
str = 1x3 string
"Mary Ann Jones" "Paul Jay Burns" "John Paul Smith"
返回一个逻辑数组,其中等于 1
的每个元素的位置对应于 str
中包含 Paul
的字符串的位置。
pat = "Paul";
TF = contains(str,pat)
TF = 1x3 logical array
0 1 1
显示包含 Paul
的字符串。使用 TF
重新索引 str
。
str(TF)
ans = 1x2 string
"Paul Jay Burns" "John Paul Smith"
使用模式进行搜索
自 R2020b 开始提供
创建一个包含地址的字符串数组。
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."
要查找包含数字的地址,请使用 digitsPattern
函数创建一个匹配任意位数字的模式。
pat = digitsPattern
pat = pattern
Matching:
digitsPattern
返回一个逻辑数组,指示哪些字符串包含数字。显示匹配的字符串。
TF = contains(str,pat)
TF = 1x3 logical array
1 0 1
str(TF)
ans = 1x2 string
"221B Baker St." "4059 Mt Lee Dr."
搜索包含一序列数字且后跟一个字母的字符串。您可以通过组合简单模式来构建更复杂的模式。
pat = digitsPattern + lettersPattern(1)
pat = pattern
Matching:
digitsPattern + lettersPattern(1)
TF = contains(str,pat); str(TF)
ans = "221B Baker St."
有关创建模式对象的函数列表,请参阅pattern
。
匹配列表中的任何文本
创建一个由人名组成的字符串数组,其中一些人名包含 Ann
或 Paul
。
str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]
str = 1x3 string
"Mary Ann Jones" "Christopher Matth..." "John Paul Smith"
查找 str
中包含 Ann
或 Paul
的元素。
pat = ["Ann","Paul"]; TF = contains(str,pat)
TF = 1x3 logical array
1 0 1
使用 TF
重新索引 str
。
str(TF)
ans = 1x2 string
"Mary Ann Jones" "John Paul Smith"
忽略大小写
创建一个包含名字的字符串数组。确定哪些名称包含 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
显示包含 anne
的字符串。使用 TF
重新索引 str
。
str(TF)
ans = 1x2 string
"Anne" "Marianne"
确定字符向量中是否包含子字符串
创建包含食品列表的字符向量。确定字符向量中是否包含不同食品的名称。
chr = 'peppers, onions, and mushrooms'; TF = contains(chr,'onion')
TF = logical
1
TF = contains(chr,'pineapples')
TF = logical
0
输入参数
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
str
和pat
必须是字符串标量、字符向量或最多包含一个字符向量的元胞数组。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅Run MATLAB Functions in Thread-Based Environment。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
用法说明和限制:
pat
必须为字符串数组、字符向量或字符向量元胞数组。
有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2016b 中推出
另请参阅
count
| endsWith
| extract
| matches
| replace
| startsWith
| split
| pattern
| digitsPattern
| lettersPattern
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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ñ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)