matlab.unittest.constraints.IsSubstringOf 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint
测试值是否为指定字符串的子字符串
描述
matlab.unittest.constraints.IsSubstringOf
类提供一个约束来测试值是否为指定字符串的子字符串。
创建对象
描述
c = matlab.unittest.constraints.IsSubstringOf(
创建一个约束来测试值是否为指定字符串的子字符串。出现在 superstring
)superstring
中的字符串标量或字符向量满足该约束。
c = matlab.unittest.constraints.IsSubstringOf(
使用一个或多个名称-值参数设置其他选项。例如,superstring
,Name,Value
)c = matlab.unittest.constraints.IsSubstringOf(superstring,"IgnoringCase",true)
创建一个不区分大小写的约束。
输入参数
superstring
— 预期的大字符串
字符串标量 | 字符向量
预期的大字符串,指定为非空字符串标量或字符向量。
此参数设置 Superstring
属性。
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
示例: c = matlab.unittest.constraints.IsSubstringOf(superstring,IgnoringCase=true)
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: c = matlab.unittest.constraints.IsSubstringOf(superstring,"IgnoringCase",true)
IgnoringCase
— 是否忽略大小写
false
或 0
(默认) | true
或 1
是否忽略大小写,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,约束区分大小写。
此参数设置 IgnoreCase
属性。
IgnoringWhitespace
— 是否忽略空白
false
或 0
(默认) | true
或 1
是否忽略空白,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,约束区分空白字符。空白字符包括空格 (' '
)、换页符 ('\f'
)、换行符 ('\n'
)、回车符 ('\r'
)、水平制表符 ('\t'
) 和垂直制表符 ('\v'
)。
此参数设置 IgnoreWhitespace
属性。
注意
当 IgnoringWhitespace
为 true
时,superstring
必须包含至少一个非空白字符。
WithCount
— 要测试的值必须出现的次数
正整数标量
要测试的值必须在 superstring
中出现的次数,指定为正整数标量。
您可以指定此名称-值参数,以便只对子字符串中不重叠的匹配项计数。例如,下面的试失败。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsSubstringOf testCase = TestCase.forInteractiveUse; testCase.verifyThat("aba",IsSubstringOf("ababa","WithCount",2))
测试框架使用 count
函数来计算要测试的值出现的次数。如果要测试的值是空字符串,则 count
函数会计算位于 superstring
的开头和结尾以及每对字符之间的空字符串的数量。换句话说,如果 superstring
包含 n 个字符,则它包含 n+1 个空子字符串。
数据类型: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
属性
Superstring
— 预期的大字符串
字符串标量 | 字符向量
IgnoreCase
— 是否忽略大小写
false
或 0
(默认) | true
或 1
是否忽略大小写,以逻辑值 0
(false
) 或 1
(true
) 形式返回。默认情况下,约束区分大小写。
此属性由 IgnoringCase
名称-值参数设置。
属性:
GetAccess | public |
SetAccess | private |
IgnoreWhitespace
— 是否忽略空白
false
或 0
(默认) | true
或 1
是否忽略空白,以逻辑值 0
(false
) 或 1
(true
) 形式返回。默认情况下,约束区分空白字符。
此属性由 IgnoringWhitespace
名称-值参数设置。
属性:
GetAccess | public |
SetAccess | private |
示例
测试值是否为指定字符串的子字符串
使用 IsSubstringOf
约束测试是否为子字符串。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsSubstringOf
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
指定预期的大字符串。
str = "This Is One Long Message!";
验证值 "One"
是 str
的子字符串。
testCase.verifyThat("One",IsSubstringOf(str))
Verification passed.
测试 "long"
是否为 str
的子字符串。测试失败,因为约束区分大小写。
testCase.verifyThat("long",IsSubstringOf(str))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsSubstringOf failed. --> The value is not found within the superstring. Actual Value: "long" Expected Superstring: "This Is One Long Message!"
测试值 "is"
是否在 str
中出现两次。要使测试通过,请忽略大小写。
testCase.verifyThat("is",IsSubstringOf(str, ... "WithCount",2,"IgnoringCase",true))
Verification passed.
测试值 "thisisone"
是否为 str
的子字符串。要使测试通过,请忽略大小写和空白字符。
testCase.verifyThat("thisisone",IsSubstringOf(str, ... "IgnoringCase",true,"IgnoringWhitespace",true))
Verification passed.
验证 "Longer"
不是 str
的子字符串。
testCase.verifyThat("Longer",~IsSubstringOf(str))
Verification passed.
版本历史记录
在 R2013a 中推出R2019b: 验证字符串出现的次数
要指定字符串标量或字符向量必须出现的次数,请使用 WithCount
名称-值参数。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)