Main Content

strrep

Find and replace substrings

Description

example

Note

replace is recommended over strrep because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality.

newStr = strrep(str,old,new) replaces all occurrences of 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

collapse all

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'

Create a string array.

str = ["the quick brown fox";
       "and the lazy dog"]
str = 2x1 string
    "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
    "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
    {'Date Received: 2016-09-06'}
    {'Date Accepted: 2016-10-11'}

Create a character vector with a repeated, overlapping pattern. Compare the results of using the strrep, replace, and regexprep 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 the strfind function. strfind finds all instances of the pattern, including instances that overlap.

indices = strfind(repeats, '22')
indices = 1×6

    11    18    19    26    27    28

Replace '22' using strrep. When you use strrep, it replaces every instance identified by strfind.

using_strrep = strrep(repeats, '22', '*')
using_strrep = 
'abc 2 def * ghi ** jkl ***'

Replace '22' using replace. It does not replace every instance that strrep replaces.

using_replace = replace(repeats, '22', '*')
using_replace = 
'abc 2 def * ghi *2 jkl **'

Replace '22' using regexprep. The results are identical to the results using the replace 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, the replace and regexprep functions replace an instance of a pattern as soon as they find it within the text.

Input Arguments

collapse all

Input text, specified as a string array, character vector, or cell array of character vectors.

Data Types: string | char | cell

Substring to replace, specified as a string array, character vector, or cell array of character vectors.

Data Types: string | char | cell

New substring, specified as a string array, character vector, or cell array of character vectors.

Data Types: string | char | cell

Algorithms

  • The strrep function does not find empty character vectors or empty strings for replacement. That is, when str and old both contain the empty character vector ('') or the empty string (""), strrep does not replace empty character vectors or strings with the contents of new.

  • Before replacing text, strrep finds all instances of old in str, like the strfind function. For overlapping patterns, strrep performs multiple replacements.

Alternative Functionality

Update code that makes use of strrep to use replace instead. For example:

Not RecommendedRecommended
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