replaceBetween

Replace substrings identified by indicators that mark their starts and ends

Syntax

  • newStr = replaceBetween(str,startStr,endStr,newText)
    example
  • newStr = replaceBetween(str,start,end,newText)
    example
  • newStr = replaceBetween(___,'Boundaries',bounds)
    example

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 an array that contains multiple pieces of text, then replaceBetween replaces substrings in each element of str. The output argument newStr is the same size as str.

example

newStr = replaceBetween(str,start,end,newText) replaces substrings in str. The substrings that are replaced occur between the positions start and end 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.

Create a string. 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.

str = string('The quick brown fox')
str = 

  string

    "The quick brown fox"

newStr = replaceBetween(str,'quick ',' fox','red')
newStr = 

  string

    "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 = string({'The quick brown fox jumps';'over the lazy dog'})
str = 

  2×1 string array

    "The quick brown fox jumps"
    "over the lazy dog"

newText = {'red';'sleeping'};
newStr = replaceBetween(str,{'quick ';'the '},{' fox';' dog'},newText)
newStr = 

  2×1 string array

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

Create a string that contains a name. To replace the middle name, specify the seventh and 11th positions in the string.

str = string('Edgar Allen Poe')
str = 

  string

    "Edgar Allen Poe"

newStr = replaceBetween(str,7,11,'A.')
newStr = 

  string

    "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 = string({'Edgar Allen Poe';'Louisa May Alcott'})
str = 

  2×1 string array

    "Edgar Allen Poe"
    "Louisa May Alcott"

newText = {'A.';'M.'};
newStr = replaceBetween(str,[7;8],[11;10],newText)
newStr = 

  2×1 string array

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

Create a string array. Replace the text between sixth and 13th positions, but do not replace the characters at those positions.

str = string('small|medium|large')
str = 

  string

    "small|medium|large"

newText = 'regular';
newStr = replaceBetween(str,6,13,newText,'Boundaries','exclusive')
newStr = 

  string

    "small|regular|large"

Replace the text between two substrings, and also the substrings themselves.

str = string('The quick brown fox jumps over the lazy dog')
str = 

  string

    "The quick brown fox jumps over the lazy dog"

newText = 'red bird flies';
newStr = replaceBetween(str,'brown','jumps',newText,'Boundaries','inclusive')
newStr = 

  string

    "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

Related Examples

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 an array with multiple pieces of text, 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 an array with multiple pieces of text, 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 an array with multiple pieces of text, then start 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 an array with multiple pieces of text, then end 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 an array with multiple pieces of text, 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 are the same data type.

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

See Also

| |

Introduced in R2016b

Was this topic helpful?