Documentation

strrep

Find and replace substring

Syntax

newStr = strrep(str,old,new)

Description

example

newStr = strrep(str,old,new)replaces all occurrences ofoldinstrwithnew

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 thestringfunction.

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 thestrrep,replace, andregexprepfunctions 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 thestrfindfunction.strfindfinds 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 thatstrrepreplaces.

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

Replace'22'usingregexprep。结果是相同的to the results using thereplacefunction.

using_regexprep = regexprep(repeats,'22','*')
using_regexprep = 'abc 2 def * ghi *2 jkl **'

strrepfinds all instances of a pattern before replacing any instance. However, thereplaceandregexprepfunctions 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.

Ifoldis 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 substring, specified as a string array, character vector, or cell array of character vectors.

Ifnewis 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

  • Thestrrepfunction 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.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?