Main Content

strcmp

Compare strings

Description

example

tf = strcmp(s1,s2) compares strings s1 and s2.

When using MATLAB® as the action language, strcmp returns 1 (true) if the two strings are identical. Otherwise, strcmp returns 0 (false).

When using C as the action language, strcmp returns 0 if the two strings are identical. Otherwise, strcmp returns a nonzero integer where:

  • The sign of the output value depends on the lexicographic order of the input strings s1 and s2.

  • The magnitude of the output value depends on the compiler that you use. This value can differ in simulation and generated code.

Strings are considered identical when they have the same size and content. strcmp is case sensitive.

example

tf = strcmp(s1,s2,n) returns 0 if the first n characters in s1 and s2 are identical in charts that use C as the action language. For this functionality when using MATLAB as the action language, see strncmp.

Examples

expand all

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

tf = strcmp("abc","abc");

Stateflow chart that uses the strcmp operator in a state.

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

tf = strcmp("abc","abcd");

Stateflow chart that uses the strcmp operator in a state.

You can compare and sort string with relational operators.

Use == to determine which elements of two string are equal. This is an alternative way to execute strcmp("abc", "abc") == 1.

"abc" == "abc"

Stateflow chart that uses the strcmp operator in a transition.

Use ~= to determine which elements of two string are not equal. This is an alternative way to execute strcmp("abc", "abc") ~= 1.

"abc" ~= "abc"

Stateflow chart that uses the strcmp operator in a transition.

Return a value of 0 if the strings are equal.

tf = strcmp("abc","abc");

Stateflow chart that uses the strcmp operator in a state.

Return a nonzero value if the strings are not equal.

tf = strcmp("abc","abcd");

Stateflow chart that uses the strcmp operator in a state.

Use == to determine which elements of two strings are equal. This is an alternative way to execute strcmp("abc", "abc") == 1.

"abc" == "abc"

Stateflow chart that uses the strcmp operator in a transition.

Use ~= to determine which elements of two strings are not equal. This is an alternative way to execute strcmp("abc", "abc") != 1.

"abc" != "abc"

Stateflow chart that uses the strcmp operator in a transition.

Return a value of 0 if the substrings are equal in charts that use C as the action language. To use this functionality when using MATLAB as the action language, see strncmp.

tf = strcmp("abc","abcd",3);

Stateflow chart that uses the strcmp operator in a state.

Introduced in R2018b