Main Content

replaceBetween

Replace substrings between start and end points

Description

example

newStr = replaceBetween(str,startStr,endStr,new) replaces the substring in str between the substrings startStr and endStr with the string new. replaceBetween does not replace startStr and endStr themselves. new can have a different number of characters than the substring it replaces.

example

newStr = replaceBetween(str,startPos,endPos,new) replaces the substring in str between the character positions startPos and endPos, including the characters at those positions.

example

newStr = replaceBetween(___,Boundaries=bounds) includes or excludes the boundaries specified in the previous syntaxes from the substring that the operator replaces. Specify bounds as "inclusive" or "exclusive".

Note

The replaceBetween operator is not supported in Stateflow® charts that use C as the action language.

Examples

expand all

Replace a substring to form the string "Hello, Mars!". By default, replaceBetween does not replace the boundary substrings.

str = "Hello, world!";
newStr = replaceBetween(str,"Hello, ","!","Mars");

Stateflow chart that uses the replaceBetween operator in a state.

Alternatively, use the option Boundaries="inclusive" to replace the boundary substrings.

str = "Hello, world!";
newStr = replaceBetween(str,"w","d","Mars",Boundaries="inclusive");

Stateflow chart that uses the replaceBetween operator in a state.

Replace a substring to form the string "Hello, Mars!". By default, replaceBetween replaces the boundary characters.

str = "Hello, world!";
newStr = replaceBetween(str,8,12,"Mars");

Stateflow chart that uses the replaceBetween operator in a state.

Alternatively, use the Boundaries="exclusive" option to prevent replacement of the boundary characters.

str = "Hello, world!";
newStr = replaceBetween(str,7,13,"Mars",Boundaries="exclusive");

Stateflow chart that uses the replaceBetween operator in a state.

Input Arguments

expand all

Input string, specified as a string scalar. Enclose literal strings with double quotes.

Example: "Hello"

Staring substring, specified as a string scalar. Enclose literal strings with double quotes.

Example: "Hello"

Ending substring, specified as a string scalar. Enclose literal strings with double quotes.

Example: "Hello"

Starting character position, specified as a positive integer.

Ending character position, specified as a positive integer.

New substring, specified as a string scalar. Enclose literal strings with double quotes.

Example: "Hello"

Boundary type, specified as "inclusive" or "exclusive". When you set bounds to "exclusive", replaceBetween replaces only the text between the boundaries. When you set bounds to "inclusive", replaceBetween also replaces the boundaries themselves.

Limitations

Version History

Introduced in R2021b