replace
Find and replace one or more substrings
Description
Examples
Replace Substring in String Array
Replace placeholder text in a list of file names.
Create a string array.
str = ["<ROOT_DIR>\MyData\data.tar.gz"; "<ROOT_DIR>\MyScripts\cleandata.m"; "<ROOT_DIR>\MyScripts\preprocess.m"; "<ROOT_DIR>\MyScripts\publishResults.m"]
str = 4x1 string
"<ROOT_DIR>\MyData\data.tar.gz"
"<ROOT_DIR>\MyScripts\cleandata.m"
"<ROOT_DIR>\MyScripts\preprocess.m"
"<ROOT_DIR>\MyScripts\publishResults.m"
Replace <ROOT_DIR>
with a string that is the name of a file path.
old = "<ROOT_DIR>"; new = "C:\MyProject"; newStr = replace(str,old,new)
newStr = 4x1 string
"C:\MyProject\MyData\data.tar.gz"
"C:\MyProject\MyScripts\cleandata.m"
"C:\MyProject\MyScripts\preprocess.m"
"C:\MyProject\MyScripts\publishResults.m"
Replace Substrings That Match Pattern
Since R2020b
Create a string that includes a phone number.
str = "Hide the numbers in: (508) 555-1234"
str = "Hide the numbers in: (508) 555-1234"
Create a pattern that matches a digit using the digitsPattern
function.
pat = digitsPattern(1)
pat = pattern
Matching:
digitsPattern(1)
Replace all digits with a "#"
character.
newStr = replace(str,pat,"#")
newStr = "Hide the numbers in: (###) ###-####"
Create another pattern that matches only phone numbers.
pat = "(" + digitsPattern(3) + ") " + digitsPattern(3) + "-" + digitsPattern(4)
pat = pattern
Matching:
"(" + digitsPattern(3) + ") " + digitsPattern(3) + "-" + digitsPattern(4)
Replace a phone number in a string that also has another number.
str = "12 calls made to: (508) 555-1234"; newStr = replace(str,pat,"(###) ###-####")
newStr = "12 calls made to: (###) ###-####"
For a list of functions that create pattern objects, see pattern
.
Replace Multiple Substrings
Replace carriage returns with newline characters.
Create a string array.
str = ["Submission Date: 11/29/15\r"; "Acceptance Date: 1/20/16\r"; "Contact: john.smith@example.com\r\n"]
str = 3x1 string
"Submission Date: 11/29/15\r"
"Acceptance Date: 1/20/16\r"
"Contact: john.smith@example.com\r\n"
Replace the carriage returns.
old = {'\r\n','\r'}; new = '\n'; newStr = replace(str,old,new)
newStr = 3x1 string
"Submission Date: 11/29/15\n"
"Acceptance Date: 1/20/16\n"
"Contact: john.smith@example.com\n"
Input Arguments
str
— Input text
string array | character vector | cell array of character vectors
Input text, specified as a string array, character vector, or cell array of character vectors.
old
— Substring to replace
string array | character vector | cell array of character vectors | pattern
array (since R2020b)
Substring to replace, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array (since R2020b)
new
— New substring
string array | character vector | cell array of character vectors
New substring, specified as a string array, character vector, or cell array of character vectors.
Tips
To perform multiple replacements for overlapping patterns, use the
strrep
function.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
replace
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
,old
, andnew
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)