Main Content

strcmp

Compare strings in Stateflow chart (case sensitive)

Description

tf = strcmp(str1,str2) compares strings str1 and str2.

  • In charts that use MATLAB® as the action language, the operator returns 1 (true) if the strings are identical and 0 (false) otherwise.

  • In charts that use C as the action language, the operator returns 0 if the strings are identical. Otherwise, it returns a nonzero integer that depends on the compiler that you use. This value can differ in simulation and generated code.

example

tf = strcmp(str1,str2,n) compares the first n characters in str1 and str2.

Note

This syntax is supported only in Stateflow® charts that use C as the action language. In charts that use MATLAB as the action language, use strncmp.

example

Examples

expand all

Return a value of 1 (true) because the strings are equal.

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

Stateflow chart that uses the strcmp operator in a state.

Return a value of 0 (false) because the strings are not equal.

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

Stateflow chart that uses the strcmp operator in a state.

You can compare strings by using relational operators. Use == to determine if two strings are equal.

[str1 == str2]

Stateflow chart that uses the strcmp operator in a transition.

Use ~= to determine if two strings are not equal.

[str1 ~= str2]

Stateflow chart that uses the strcmp operator in a transition.

Return a value of 0 because the strings are equal.

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

Stateflow chart that uses the strcmp operator in a state.

Return a nonzero value because the strings are not equal.

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

Stateflow chart that uses the strcmp operator in a state.

You can compare strings by using relational operators. Use == to determine if two strings are equal.

[str1 == str2]

Stateflow chart that uses the strcmp operator in a transition.

Use != or ~= to determine if two strings are not equal.

[str1 != str2]

Stateflow chart that uses the strcmp operator in a transition.

Return a value of 0 because the strings start with the same five characters.

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

Stateflow chart that uses the strcmp operator in a state.

This syntax is supported only in Stateflow charts that use C as the action language. In charts that use MATLAB as the action language, use strncmp.

Input Arguments

expand all

Input strings, specified as string scalars. In charts that use MATLAB as the action language, enclose literal strings with double quotes.

Example: "Hello"

Number of characters checked, starting at the beginning of each string, specified as a positive integer.

Version History

Introduced in R2018b