Main Content

replace

Find and replace substrings

Description

example

newStr = replace(str,old,new) replaces instances of the substring old that occur in the string str with the string new. Use this operator in the Requirements Table block.

Examples

expand all

In a Requirements Table block, create a requirement that replaces the substring "Hello" with the substring "Howdy" in the string "Hello, world!".

y = replace("Hello, world!","Hello","Howdy")

This image shows a requirement that takes the input string "Hello, world" and replaces the substring "Hello" with the substring "Howdy".

Input Arguments

expand all

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

Example: "Hello"

Data Types: string

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

Example: "Hello"

Data Types: string

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

Example: "Hello"

Data Types: string

Output Arguments

expand all

Output string, returned as a string scalar.

Limitations

  • This operator does not support the use of Simulink.Bus object fields.

Algorithms

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

Version History

Introduced in R2022b