replaceBetween

Replace substrings between start and end points

Description

example

newStr = replaceBetween(str,startStr,endStr,newText) replaces substrings in str with the text in newText. The substrings that are replaced occur between the substrings startStr and endStr. However, replaceBetween does not replace startStr and endStr 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 of str. The output argument newStr has the same data type as str.

example

newStr = replaceBetween(str,startPos,endPos,newText) replaces substrings in str. The substrings that are replaced occur between the positions startPos and endPos in str, including the characters at those positions.

example

newStr = replaceBetween(___,'Boundaries',bounds) 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,startStr,endStr,newText,'Boundaries','inclusive') replaces startStr, endStr, and all the text between them with the text specified by newText.

Examples

collapse all

Create string arrays and replace text that occurs between substrings.

Starting in R2017a, 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"

Create string arrays and replace substrings between start and end positions that are specified as numbers.

Starting in R2017a, 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.

str = "Edgar Allen Poe"
str = 
"Edgar Allen Poe"
newStr = replaceBetween(str,7,11,'A.')
newStr = 
"Edgar A. Poe"

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

Starting in R2017a, 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"

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

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Data Types: string | char | cell

String that indicates the start of the substring to replace, specified as a string array, a character vector, or a cell array of character vectors.

If str is a string array or a cell array of character vectors, then startStr can be a character vector, a string scalar, or a string array or a cell array of the same size as str.

Example: replaceBetween(str,"AB","YZ",newText) replaces the substring between AB and YZ in each element of str with the text specified by newText.

Example: If str is a 2-by-1 string array, then replaceBetween(str,["AB";"FG"],["YZ";"ST"],newText) replaces the substrings between AB and YZ in str(1), and between FG and ST in str(2).

Data Types: string | char | cell

Substring that indicates the end of the text to replace, specified as a string array, a character vector, or a cell array of character vectors.

If str is a string array or a cell array of character vectors, then endStr can be a character vector, a string scalar, or a string array or a cell array of the same size as str.

Example: replaceBetween(str,"AB","YZ",newText) replaces the substring between AB and YZ in each element of str with the text specified by newText.

Example: If str is a 2-by-1 string array, then replaceBetween(str,["AB";"FG"],["YZ";"ST"],newText) replaces the substrings between AB and YZ in str(1), and between FG and ST in str(2).

Data Types: string | char | cell

Start position of substring to replace, 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 a 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 of str with the text specified by newText.

Example: If str is a 2-by-1 string array, then replaceBetween(str,[5;10],[9;21],newText) replaces the substring from the fifth through the ninth positions in str(1), and the 10th through the 21st positions in str(2).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

End position of substring to replace, 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 a 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 of str with the text specified by newText.

Example: If str is a 2-by-1 string array, then replaceBetween(str,[5;10],[9;21],newText) replaces the substring from the fifth through the ninth positions in str(1), and the 10th through the 21st positions in str(2).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Replacement text, specified as a string array, a character vector, or a cell array of character vectors.

If str is a string array or a 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 as str.

Example: replaceBetween(str,"AB","YZ","efg") replaces the substring between AB and YZ in each element of str with efg.

Example: If str is a 2-by-1 string array, then replaceBetween(str,["AB";"FG"],["YZ";"ST"],["efg";"lmnop"]) replaces the substrings between AB and YZ in str(1) with efg, and between FG and ST in str(2) with lmnop.

Data Types: string | char | cell

Output Arguments

collapse all

Output text, returned as a string array, a character vector, or a cell array of character vectors. str and newStr have the same data type.

Data Types: string | char | cell

Extended Capabilities

Introduced in R2016b