Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

strcmp

比较字符串(区分大小写)

描述

示例

tf = strcmp(str1,str2) 比较字符串 str1str2

  • 在使用 MATLAB® 作为动作语言的图中,如果字符串相同,则运算符返回 1 (true),否则返回 0 (false)。

  • 在使用 C 语言作为动作语言的图中,如果字符串相同,运算符将返回 0。否则,它将返回非零整数,具体取决于您使用的编译器。此值在仿真中和生成的代码中可能不同。

示例

tf = strcmp(str1,str2,n) 比较 str1str2 中的前 n 个字符。

注意

只有使用 C 语言作为动作语言的 Stateflow® 图才支持此语法。在使用 MATLAB 作为动作语言的图中,请使用 strncmp

示例

全部展开

由于字符串相等,返回值 1 (true)。

x = strcmp("Hello","Hello");

Stateflow chart that uses the strcmp operator in a state.

由于字符串不相等,返回值 0 (false)。

y = strcmp("Hello","Hello!");

Stateflow chart that uses the strcmp operator in a state.

您可以使用关系运算符来比较字符串。使用 == 确定两个字符串是否相等。

[str1 == str2]

Stateflow chart that uses the strcmp operator in a transition.

使用 ~= 确定两个字符串是否不等。

[str1 ~= str2]

Stateflow chart that uses the strcmp operator in a transition.

由于字符串相等,返回值 0

x = strcmp("Hello","Hello");

Stateflow chart that uses the strcmp operator in a state.

由于字符串不相等,返回非零值。

y = strcmp("Hello","Hello!");

Stateflow chart that uses the strcmp operator in a state.

您可以使用关系运算符来比较字符串。使用 == 确定两个字符串是否相等。

[str1 == str2]

Stateflow chart that uses the strcmp operator in a transition.

使用 !=~= 来确定两个字符串是否不等。

[str1 != str2]

Stateflow chart that uses the strcmp operator in a transition.

返回值 0,因为字符串以相同的五个字符开头。

z = strcmp("Hello","Hello!",5);

Stateflow chart that uses the strcmp operator in a state.

只有使用 C 语言作为动作语言的 Stateflow 图才支持此语法。在使用 MATLAB 作为动作语言的图中,请使用 strncmp

输入参数

全部展开

输入字符串,指定为字符串标量。在使用 MATLAB 作为动作语言的图中,需要用双引号将文字字符串引起来。

示例: "Hello"

从每个字符串的开头开始检查的字符数,指定为正整数。

版本历史记录

在 R2018b 中推出