strrep
Find and replace substrings
Description
Note
replace
is recommended over
strrep
because it provides greater flexibility and
allows vectorization. For additional information, see Alternative Functionality.
newStr = strrep(
replaces all occurrences of str
,old
,new
)old
in str
with
new
.
If any input argument is a nonscalar string array or cell array of character vectors, then the other input arguments must have compatible sizes.
Examples
Input Arguments
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.
Alternative Functionality
Update code that makes use of strrep
to use replace
instead. For example:
Not Recommended | Recommended |
---|---|
str = "ababa; newstr = strrep(str,"b","c") newstr = "acaca" |
str = "ababa; newstr = replace(str,"b","c") newstr = "acaca" |
Extended Capabilities
Version History
Introduced before R2006a