Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

strrep

Find and replace substring

Syntax

newStr = strrep(str,old,new)

Description

example

newStr = strrep(str,old,new) replaces all occurrences of old in str with new.

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. Starting in R2017a, you can create strings using double quotes instead of the string 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'}

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 = 

    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.

If old is a nonscalar string or cell array, then it must be the same size as str. Otherwise, it can be a character vector or string scalar.

Data Types: string | char | cell

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

If new is a nonscalar string or cell array, then it must be the same size as str. 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, 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.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?