strfind
在其他字符串中查找字符串
说明
当 cellOutput
为 true 时,即使 str
是字符向量,
也会强制 k
= strfind(str
,pat
,'ForceCellOutput',cellOutput
)strfind
以元胞数组形式返回 k
。
示例
在字符向量中查找子字符串
在字符向量中查找子字符串的起始索引。
首先,创建一个字符向量。
str = 'Find the starting indices of substrings in a character vector';
查找子字符串 in
。
k = strfind(str,'in')
k = 1×5
2 15 19 36 41
str
中有五个实例。
查找子字符串 In
。
k = strfind(str,'In')
k = []
由于 strfind
区分大小写,未找到子字符串。k
是一个空数组。
在 str
中查找空白。
k = strfind(str,' ')
k = 1×9
5 9 18 26 29 40 43 45 55
str
中有十处空白。
使用模式查找字母和单词
自 R2020b 开始提供
创建字符向量。
str = 'Find the letters.'
str = 'Find the letters.'
使用 lettersPattern
函数创建匹配字母序列的模式。
pat = lettersPattern
pat = pattern
Matching:
lettersPattern
查找每个字母的索引。当 pat
匹配任意长度的字母序列时,strfind
会在找到一个匹配项时立即停止,然后继续下一个匹配。例如,'Find'
和 'F'
都与 lettersPattern
匹配,因为没有指定匹配的字母数。但是,strfind
首先匹配 'F'
,并返回其索引。然后 strfind
匹配 'i'
,依此类推。(您可以调用带可选参数(指定要匹配的字母数)的 lettersPattern
。)
k = strfind(str,pat)
k = 1×14
1 2 3 4 6 7 8 10 11 12 13 14 15 16
要查找单词的开头,请调用带边界的 lettersPattern
。letterBoundary
函数匹配字母和非字母字符之间的边界。
pat = letterBoundary + lettersPattern
pat = pattern
Matching:
letterBoundary + lettersPattern
k = strfind(str,pat)
k = 1×3
1 6 10
有关创建模式对象的函数列表,请参阅pattern
。
在元胞数组中查找子字符串
在字符向量元胞数组中查找子字符串的起始索引。
创建字符向量元胞数组。
str = {'How much wood would a woodchuck chuck'; 'if a woodchuck could chuck wood?'};
在 str
中查找 wood
。
idx = strfind(str,'wood')
idx=2×1 cell array
{[10 23]}
{[ 6 28]}
检查输出元胞数组以查找 wood
的实例。
idx{:,:}
ans = 1×2
10 23
ans = 1×2
6 28
子字符串 wood
在第一个字符向量的索引 10 和 23 处出现,在第二个字符向量的索引 6 和 28 处出现。
以元胞数组形式返回索引
在字符向量中查找子字符串的所有出现处。强制 strfind
以元胞数组形式返回这些出现处的索引。然后显示这些索引。
创建一个字符向量并查找 ain
模式的所有出现处。
str = 'The rain in Spain.'; k = strfind(str,'ain','ForceCellOutput',true)
k = 1x1 cell array
{[6 15]}
strfind
返回一个包含数值数组的标量元胞,该数值数组包含子字符串 ain
在 str
中的所有出现处的索引。要访问元胞中的数值数组,请使用花括号。
k{1}
ans = 1×2
6 15
输入参数
输出参数
提示
如果
pat
是不包含任何字符的字符向量或字符串标量(''
或""
),则strfind
将返回一个空数组。建议使用
contains
函数在字符串数组中查找模式。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
用法说明和限制:
文本输入必须是字符串 tall 数组或字符向量 tall 元胞数组。
模式输入必须为单个字符串,并且不能为 tall 数组或模式对象。
输出是索引向量的 tall 元胞数组,每个输入字符串对应一个元素。
有关详细信息,请参阅tall 数组。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
str
必须是字符串标量或字符向量。生成的代码以 1×0 字符数组的形式返回空输出。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
用法说明和限制:
str
必须为字符串数组或字符向量元胞数组。
有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2006a 之前推出
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)