Main Content

replaceBetween

Replace substrings between start and end points

Description

example

newStr = replaceBetween(str,startPat,endPat,newText) replaces the substring in str with the text in newText. replaceBetween does not replace startPat and endPat themselves. The newText argument can have a different number of characters than the substring it replaces.

example

newStr = replaceBetween(str,startPos,endPos,newText) replaces the substring in str, including the characters at those positions.

example

newStr = replaceBetween(___,'Boundaries',bounds) forces the boundaries specified in the previous syntaxes to be inclusive or exclusive. The boundaries 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.

Note

Only Stateflow® charts that use MATLAB® as the action language support the replacebetween operator.

Examples

expand all

h = "Hello, world!"
startPat = "H"
endPat = ","
newText = "owdy"
x = replaceBetween(h, startPat, endPat, newText)

Stateflow chart that uses the replacebetween operator in a state.

Results from stateflow chart that uses the replacebetween operator in a state.

h = "Hello, world!"
newText = "owdy"
x = replaceBetween(h, 2, 6, newText)

Stateflow chart that uses the replacebetween operator in a state.

Results from stateflow chart that uses the replacebetween operator in a state.

h = "Hello, world!"
startPat = "H"
endPat = "o"
newText = "Howdy"
x = replaceBetween(h, startPat, endPat, newText, 'Boundaries','inclusive')

Stateflow chart that uses the replacebetween operator in a state.

Results from stateflow chart that uses the replacebetween operator in a state.

h = "Hello, world!"
startPat = "H"
endPat = ","
newText = "owdy"
x = replaceBetween(h, startPat, endPat, newText, 'Boundaries', 'exclusive')

Stateflow chart that uses the replacebetween operator in a state.

Results from stateflow chart that uses the replacebetween operator in a state.

Limitations

Introduced in R2021b