Main Content

strrep

Find and replace substrings

Since R2021b

Description

example

newStr = strrep(str,old,new) replaces instances of the substring old that occur in the string str with the string new.

Note

The strrep 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!".

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

Stateflow chart that uses the strrep operator in a state.

Input Arguments

expand all

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

Example: "Hello"

Substring to replace, specified as a string scalar. Enclose literal strings with double quotes.

Example: "Hello"

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

Example: "Hello"

Limitations

Algorithms

The strrep operator replaces overlapping substrings. For example, strrep("abc 2 def 22 ghi 222 jkl 2222","22","*") returns "abc 2 def * ghi ** jkl ***". To replace only sequential substrings, use replace. For more information, see Replace Repeated Pattern.

Version History

Introduced in R2021b