Main Content

replaceBetween

Replace substrings between start and end points

Description

example

newStr = replaceBetween(str,startPat,endPat,newText) replaces substrings in str with the text in newText. 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 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,startPat,endPat,newText,'Boundaries','inclusive') replaces startPat, endPat, 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"

If you are using R2016b, create string arrays using the string function instead of double quotes.

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"

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.

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, character vector, or cell array of character vectors.

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 of str. You can specify that the substrings either all have the same start or have different starts in each element of str.

  • To specify the same start, specify startPat as a character vector, string scalar, or pattern object.

  • To specify different starts, specify startPat as a string array, cell array of character vectors, or pattern array.

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

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 of str. You can specify that the substrings either all have the same end or have different ends in each element of str.

  • To specify the same end, specify endPat as a character vector, string scalar, or pattern object.

  • To specify different ends, specify endPat as a string array, cell array of character vectors, or pattern array.

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

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

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

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

Output Arguments

collapse all

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

Introduced in R2016b