strrep
查找并替换子字符串
说明
示例
替换字符向量中的文本
创建字符向量并替换其中的子字符串。
chr = 'The quick brown fox'
chr = 'The quick brown fox'
newChr = strrep(chr,'quick','sly')
newChr = 'The sly brown fox'
替换字符串数组中的文本
创建一个字符串数组。
str = ["the quick brown fox"; "and the lazy dog"]
str = 2x1 string
"the quick brown fox"
"and the lazy dog"
替换每个数组元素中的子字符串。
newStr = strrep(str,'the','a')
newStr = 2x1 string
"a quick brown fox"
"and a lazy dog"
将子字符串替换为多个值
将元胞数组中的占位符内容 '___'
替换为第二个元胞数组中的不同值。
C1 = {'Date Received: ___'; 'Date Accepted: ___'}; old = '___'; new = {'2016-09-06'; '2016-10-11'}; C2 = strrep(C1,old,new)
C2 = 2x1 cell
{'Date Received: 2016-09-06'}
{'Date Accepted: 2016-10-11'}
替换重复模式
创建具有重复、重叠模式的字符向量。使用 strrep
、replace
和 regexprep
函数比较结果以取代该模式。
repeats = 'abc 2 def 22 ghi 222 jkl 2222'
repeats = 'abc 2 def 22 ghi 222 jkl 2222'
使用 strfind
函数查找重复模式 '22'
的索引。strfind
会查找模式的所有实例,包括重叠的实例。
indices = strfind(repeats, '22')
indices = 1×6
11 18 19 26 27 28
使用 strrep
替换 '22'
。当您使用 strrep
时,它会替换 strfind
标识的每个实例。
using_strrep = strrep(repeats, '22', '*')
using_strrep = 'abc 2 def * ghi ** jkl ***'
使用 replace
替换 '22'
。它不会替换 strrep
替换的每个实例。
using_replace = replace(repeats, '22', '*')
using_replace = 'abc 2 def * ghi *2 jkl **'
使用 regexprep
替换 '22'
。结果与使用 replace
函数的结果相同。
using_regexprep = regexprep(repeats, '22', '*')
using_regexprep = 'abc 2 def * ghi *2 jkl **'
strrep
在替换任何实例之前先查找模式的所有实例。但是,replace
和 regexprep
函数在文本中找到模式的实例后会立即进行替换。
输入参数
str
— 输入文本
字符串数组 | 字符向量 | 字符向量元胞数组
输入文本,指定为字符串数组、字符向量或字符向量元胞数组。
数据类型: string
| char
| cell
old
— 要替换的子字符串
字符串数组 | 字符向量 | 字符向量元胞数组
要替换的子字符串,指定为字符串数组、字符向量或字符向量元胞数组。
数据类型: string
| char
| cell
new
— 新的子字符串
字符串数组 | 字符向量 | 字符向量元胞数组
新的子字符串,指定为字符串数组、字符向量或字符向量元胞数组。
数据类型: string
| char
| cell
算法
strrep
函数不会查找空字符向量或空字符串进行替换。即,当str
和old
均包含空字符向量 (''
) 或空字符串 (""
) 时,strrep
不会将空字符向量或字符串替换为new
的内容。在替换文本之前,
strrep
会像strfind
函数一样查找str
中的所有old
实例。对于重叠模式,strrep
执行多次替换。
扩展功能
tall 数组
对行数太多而无法放入内存的数组进行计算。
此函数支持 tall 数组,但存在以下限制:
原始字符串必须是字符串 tall 数组或字符向量 tall 元胞数组。
旧字符串和新字符串输入可以是单个字符串或具有相同大小的字符串 tall 数组。
有关详细信息,请参阅 使用 tall 数组处理无法放入内存的数据。
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
str
、old
和new
必须是字符串标量或字符向量。生成的代码以 1×0 字符数组的形式返回空输出。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
用法说明和限制:
输入必须为字符串数组或字符向量元胞数组。
有关详细信息,请参阅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)