Main Content

本页采用了机器翻译。点击此处可查看英文原文。

replace

查找并替换 Requirements Table模块中的子字符串

自 R2022b 起

描述

示例

newStr = replace(str,old,new) 将字符串 str 中出现的子字符串 old 的实例替换为字符串 new。在 Requirements Table模块中使用此运算符。

示例

全部展开

Requirements Table模块中,创建一个需求,将字符串 "Hello, world!" 中的子字符串 "Howdy" 替换为子字符串 "Hello"

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

输入参数

全部展开

输入字符串,指定为字符串标量。将文字字符串用双引号引起来。

示例: "Hello"

数据类型: string

要替换的子字符串,指定为字符串标量。将文字字符串用双引号引起来。

示例: "Hello"

数据类型: string

新的子字符串,指定为字符串标量。将文字字符串用双引号引起来。

示例: "Hello"

数据类型: string

输出参量

全部展开

输出字符串,以字符串标量形式返回。

限制

  • 该运算符不支持使用Simulink.Bus对象字段。

算法

replace 运算符替换连续的子字符串。例如,replace("abc 2 def 22 ghi 222 jkl 2222","22","*") 返回 "abc 2 def * ghi *2 jkl **"。要替换重叠的子字符串,请使用 strrep。有关更多信息,请参阅 替换重复模式

版本历史记录

在 R2022b 中推出

另请参阅

|