strrep
Find and replace substring
Syntax
newStr = strrep(str,old,new)
Description
Examples
Replace Text in Character Vector
Create a character vector and replace a substring within it.
chr ='The quick brown fox'
chr = 'The quick brown fox'
newChr = strrep(chr,'quick','sly')
newChr = 'The sly brown fox'
Replace Text in String Array
Create a string array. Starting in R2017a, you can create strings using double quotes instead of thestring
function.
str = ["the quick brown fox";"and the lazy dog"]
str =2x1 string array"the quick brown fox" "and the lazy dog"
Replace a substring in each element of the array.
newStr = strrep(str,'the','a')
newStr =2x1 string array"a quick brown fox" "and a lazy dog"
子字符串替换为多个值
Replace placeholder content in a cell array,'___'
, with different values in a second cell array.
C1 = {'Date Received: ___';'Date Accepted: ___'}; old ='___'; new = {'2016-09-06';'2016-10-11'}; C2 = strrep(C1,old,new)
C2 =2x1 cell array{'Date Received: 2016-09-06'} {'Date Accepted: 2016-10-11'}
Replace Repeated Pattern
Create a character vector with a repeated, overlapping pattern. Compare the results of using thestrrep
,replace
, andregexprep
functions to replace the pattern.
repeats ='abc 2 def 22 ghi 222 jkl 2222'
repeats = 'abc 2 def 22 ghi 222 jkl 2222'
Find the indices of the repeating pattern'22'
using thestrfind
function.strfind
finds all instances of the pattern, including instances that overlap.
指数=strfind(repeats,'22')
指数=11 18 19 26 27 28
Replace'22'
usingstrrep
。When you usestrrep
, it replaces every instance identified bystrfind
。
using_strrep = strrep(repeats,'22','*')
using_strrep = 'abc 2 def * ghi ** jkl ***'
Replace'22'
usingreplace
。It does not replace every instance thatstrrep
replaces.
using_replace = replace(repeats,'22','*')
using_replace = 'abc 2 def * ghi *2 jkl **'
Replace'22'
usingregexprep
。结果是相同的to the results using thereplace
function.
using_regexprep = regexprep(repeats,'22','*')
using_regexprep = 'abc 2 def * ghi *2 jkl **'
strrep
finds all instances of a pattern before replacing any instance. However, thereplace
andregexprep
functions replace an instance of a pattern as soon as they find it within the text.
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.
Data Types:string
|char
|cell
old
—Substring to replace
string array|character vector|cell array of character vectors
Substring to replace, specified as a string array, character vector, or cell array of character vectors.
Ifold
is a nonscalar string or cell array, then it must be the same size asstr
。Otherwise, it can be a character vector or string scalar.
Data Types:string
|char
|cell
new
—New substring
string array|character vector|cell array of character vectors
New substring, specified as a string array, character vector, or cell array of character vectors.
Ifnew
is a nonscalar string or cell array, then it must be the same size asstr
。Otherwise, it can be a character vector or string scalar.
Data Types:string
|char
|cell
Algorithms
The
strrep
function does not find empty character vectors or empty strings for replacement. That is, whenstr
andold
both contain the empty character vector (''
) or the empty string(""
),strrep
does not replace empty character vectors or strings with the contents ofnew
。Before replacing text,
strrep
finds all instances ofold
instr
, like thestrfind
function. For overlapping patterns,strrep
performs multiple replacements.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitations:
The original string must be a tall array of strings or a tall cell array of character vectors.
The old string and new string inputs can be single strings or tall arrays of strings with the same size.
For more information, seeTall Arrays。
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
,old
, andnew
must be a string scalar or a character vector.Generated code returns an empty output as a 1-by-0 character array.
See Also
Introduced before R2006a
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ñ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)