Create a string. Replace the substring mary with anne.
str = "Rosemary Jones"
str =
"Rosemary Jones"
newStr = replace(str,"mary","anne")
newStr =
"Roseanne Jones"
You can also replace text using the strrep function. However, the replace function is
recommended.
newStr = strrep(str,"Jones","Day")
newStr =
"Rosemary Day"
Create a string array that contains many names.
str = ["Rosemary Ann Jones","Peter Michael Smith","Ann Marie Young"]
str = 1x3 string array
"Rosemary Ann Jones" "Peter Michael Smith" "Ann Marie Young"
Specify multiple names to replace.
oldText = ["Ann","Michael"];
newText = ["Beth","John"];
newStr = replace(str,oldText,newText)
newStr = 1x3 string array
"Rosemary Beth Jones" "Peter John Smith" "Beth Marie Young"
Replace text in a character vector. You can use replace and replaceBetween with
character vectors, as well as with strings.
chr = 'Mercury, Gemini, Apollo'
chr =
'Mercury, Gemini, Apollo'
replace(chr,'Gemini','Mars')
Search and Replace Text