replaceBetween - Replace substrings between start and end points - MATLAB (original) (raw)
Replace substrings between start and end points
Syntax
Description
[newStr](#bvdtgwk-1-newStr) = replaceBetween([str](#bvdtgwk-1%5Fsep%5Fbu4l86d-str),[startPat](#bvdtgwk-1-startStr),[endPat](#bvdtgwk-1-endStr),[newText](#bvdtgwk-1-newText))
replaces substrings in str
with the text innewText
. The substrings that are replaced occur between the substrings startPat
and endPat
. However,replaceBetween
does not replace startPat
and endPat
themselves. replaceBetween
returns the result as newStr
. The newText
argument can have a different number of characters than the substring it replaces.
If str
is a string array or a cell array of character vectors, then replaceBetween
replaces substrings in each element ofstr
. The output argument newStr
has the same data type as str
.
[newStr](#bvdtgwk-1-newStr) = replaceBetween([str](#bvdtgwk-1%5Fsep%5Fbu4l86d-str),[startPos](#bvdtgwk-1-startPos),[endPos](#bvdtgwk-1-endPos),[newText](#bvdtgwk-1-newText))
replaces substrings in str
. The substrings that are replaced occur between the positions startPos
andendPos
in str
, including the characters at those positions.
[newStr](#bvdtgwk-1-newStr) = replaceBetween(___,'Boundaries',[bounds](#mw%5Facccfecc-6b12-4090-9a6d-04ca2b530dcf))
forces the starts and ends specified in any of the previous syntaxes to be either inclusive or exclusive. They are inclusive when bounds
is'inclusive'
, and exclusive when bounds
is'exclusive'
. For example,replaceBetween(str,startPat,endPat,newText,'Boundaries','inclusive')
replaces startPat
, endPat
, and all the text between them with the text specified by newText
.
Examples
Replace Text Between Substrings
Create string arrays and replace text that occurs between substrings.
You can create strings using double quotes.
str = "The quick brown fox"
str = "The quick brown fox"
Replace the text that occurs between the substrings quick
and fox
. The replaceBetween
function replaces the text but does not replace quick
or fox
in the output.
newStr = replaceBetween(str,"quick "," fox","red")
newStr = "The quick red fox"
Replace substrings from each element of a string array. When you specify different substrings as start and end indicators, they must be contained in a string array or a cell array that is the same size as str
. The replacement text also must be in a string array or a cell array of the same size.
str = ["The quick brown fox jumps";"over the lazy dog"]
str = 2x1 string "The quick brown fox jumps" "over the lazy dog"
newText = ["red";"sleeping"]; newStr = replaceBetween(str,["quick ";"the "],[" fox";" dog"],newText)
newStr = 2x1 string "The quick red fox jumps" "over the sleeping dog"
Replace Text Between Specified Patterns
Since R2020b
Create abbreviations like "i18n"
for "internationalization"
. To do this, replace the text between the first and last letters of a word with the number of letters.
First, create a string array.
str = ["globalization"; "internationalization"; "localization"]
str = 3x1 string "globalization" "internationalization" "localization"
Create patterns that match the first and last characters of a string. The textBoundary
function matches boundaries at the start or end of a string, while the call lettersPattern(1)
matches any letter.
startPat = textBoundary + lettersPattern(1)
startPat = pattern Matching:
textBoundary + lettersPattern(1)
endPat = lettersPattern(1) + textBoundary
endPat = pattern Matching:
lettersPattern(1) + textBoundary
Find the length of each string in str
. To account for the first and last letters, subtract two from each length. Convert the lengths to strings.
L = strlength(str) - 2; L = string(L)
L = 3x1 string "11" "18" "10"
Create abbreviations for the elements of str
. Replace the characters between the first and last letters with the elements of L
.
newStr = replaceBetween(str,startPat,endPat,L)
newStr = 3x1 string "g11n" "i18n" "l10n"
For a list of functions that create pattern objects, see pattern.
Replace Substrings Between Start and End Positions
Create string arrays and replace substrings between start and end positions that are specified as numbers.
You can create strings using double quotes. Create a string that contains a name. To replace the middle name, specify the seventh and 11th positions in the string.
newStr = replaceBetween(str,7,11,'A.')
Replace substrings from each element of a string array. When you specify different start and end positions with numeric arrays, they must be the same size as the input string array. The replacement text also must be in a string array or a cell array of the same size.
str = ["Edgar Allen Poe";"Louisa May Alcott"]
str = 2x1 string "Edgar Allen Poe" "Louisa May Alcott"
newText = ["A.";"M."]; newStr = replaceBetween(str,[7;8],[11;10],newText)
newStr = 2x1 string "Edgar A. Poe" "Louisa M. Alcott"
Replace Text Within Inclusive and Exclusive Boundaries
Replace text from string arrays within boundaries that are forced to be inclusive or exclusive. replaceBetween
replaces the boundaries along with the text when the boundaries are inclusive. replaceBetween
does not replace the boundaries when the boundaries are exclusive.
You can create strings using double quotes.
str = "small|medium|large"
str = "small|medium|large"
Replace the text between sixth and 13th positions, but do not replace the characters at those positions.
newText = "regular"; newStr = replaceBetween(str,6,13,newText,'Boundaries','exclusive')
newStr = "small|regular|large"
Replace the text between two substrings, and also the substrings themselves.
str = "The quick brown fox jumps over the lazy dog"
str = "The quick brown fox jumps over the lazy dog"
newText = "red bird flies"; newStr = replaceBetween(str,'brown','jumps',newText,'Boundaries','inclusive')
newStr = "The quick red bird flies over the lazy dog"
Replace Text Between Positions in Character Vector
Create a character vector and replace text between start and end positions.
chr = 'mushrooms, peppers, and onions'
chr = 'mushrooms, peppers, and onions'
newChr = replaceBetween(chr,12,18,'pineapple')
newChr = 'mushrooms, pineapple, and onions'
Replace text between substrings.
newChr = replaceBetween(chr,'mushrooms,',' and',' extra cheese,')
newChr = 'mushrooms, extra cheese, and onions'
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.
startPat
— Text or pattern that marks start position
string array | character vector | cell array of character vectors | pattern
array (since R2020b)
Text or pattern that marks the start position of the text to replace, specified as one of the following:
- String array
- Character vector
- Cell array of character vectors
- pattern array (since R2020b)
If str
is a string array or cell array of character vectors, then you can replace substrings in every element ofstr
. You can specify that the substrings either all have the same start or have different starts in each element ofstr
.
- To specify the same start, specify
startPat
as a character vector, string scalar, orpattern
object. - To specify different starts, specify
startPat
as a string array, cell array of character vectors, orpattern
array.
Example: replaceBetween(str,"AB","YZ",newText)
replaces the substring between AB
and YZ
in each element of str
with the text specified bynewText
.
Example: If str
is a2
-by-1
string array, thenreplaceBetween(str,["AB";"FG"],["YZ";"ST"],newText)
replaces the substrings between AB
andYZ
in str(1)
, and betweenFG
and ST
instr(2)
.
endPat
— Text or pattern that marks end position
string array | character vector | cell array of character vectors | pattern
array (since R2020b)
Text or pattern that marks the end position of the text to replace, specified as one of the following:
- String array
- Character vector
- Cell array of character vectors
- pattern array (since R2020b)
If str
is a string array or cell array of character vectors, then you can replace substrings in every element ofstr
. You can specify that the substrings either all have the same end or have different ends in each element ofstr
.
- To specify the same end, specify
endPat
as a character vector, string scalar, orpattern
object. - To specify different ends, specify
endPat
as a string array, cell array of character vectors, orpattern
array.
Example: replaceBetween(str,"AB","YZ",newText)
replaces the substring between AB
and YZ
in each element of str
with the text specified bynewText
.
Example: If str
is a2
-by-1
string array, thenreplaceBetween(str,["AB";"FG"],["YZ";"ST"],newText)
replaces the substrings between AB
andYZ
in str(1)
, and betweenFG
and ST
instr(2)
.
startPos
— Start position
numeric array
Start position, specified as a numeric array.
If str
is a string array or a cell array of character vectors, then startPos
can be a numeric scalar or numeric array of the same size as str
.
Example: replaceBetween(str,5,9,newText)
replaces the substring from the fifth to the ninth positions in each element ofstr
with the text specified bynewText
.
Example: If str
is a2
-by-1
string array, thenreplaceBetween(str,[5;10],[9;21],newText)
replaces the substring from the fifth through the ninth positions instr(1)
, and the 10th through the 21st positions instr(2)
.
endPos
— End position
numeric array
End position, specified as a numeric array.
If str
is a string array or a cell array of character vectors, then endPos
can be a numeric scalar or numeric array of the same size as str
.
Example: replaceBetween(str,5,9,newText)
replaces the substring from the fifth to the ninth positions in each element ofstr
with the text specified bynewText
.
Example: If str
is a2
-by-1
string array, thenreplaceBetween(str,[5;10],[9;21],newText)
replaces the substring from the fifth through the ninth positions instr(1)
, and the 10th through the 21st positions instr(2)
.
newText
— Replacement text
string array | character vector | cell array of character vectors
Replacement text, specified as a string array, a character vector, or a cell array of character vectors.
If str
is a string array or cell array of character vectors, then newText
can be a character vector, a string scalar, or a string array or a cell array of the same size asstr
.
Example: replaceBetween(str,"AB","YZ","efg")
replaces the substring between AB
and YZ
in each element of str
withefg
.
Example: If str
is a2
-by-1
string array, thenreplaceBetween(str,["AB";"FG"],["YZ";"ST"],["efg";"lmnop"])
replaces the substrings between AB
andYZ
in str(1)
withefg
, and between FG
andST
in str(2)
withlmnop
.
bounds
— Boundary behavior
'inclusive'
| 'exclusive'
Boundary behavior, specified as 'inclusive'
or'exclusive'
. When boundary behavior is inclusive the start and end specified by previous arguments are included in the replaced text. If boundary behavior is exclusive, then the start and end are not included.
Output Arguments
newStr
— Output text
string array | character vector | cell array of character vectors
Output text, returned as a string array, character vector, or cell array of character vectors. str
and newStr
have the same data type.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
ThereplaceBetween
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
,startPat
,endPat
, andnewText
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