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
strrepfunction does not find empty character vectors or empty strings for replacement. That is, whenstrandoldboth contain the empty character vector ('') or the empty string (""),strrepdoes not replace empty character vectors or strings with the contents ofnew.Before replacing text,
strrepfinds all instances ofoldinstr, like thestrfindfunction. For overlapping patterns,strrepperforms 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