Main Content


Compare first N characters of strings in Stateflow chart (case sensitive)

Since R2021b



tf = strncmp(str1,str2,n) compares the first n characters of str1 and str2. The operator returns 1 (true) if the strings are identical and 0 (false) otherwise.


The operator strncmp is not supported in Stateflow® charts that use C as the action language. For similar functionality, use strcmp.


expand all

Set x to 1 (true) because the first 13 characters in the strings match. Set y to 0 (false) because the first 14 characters in the strings do not match.

str1 = "Hello, world!";
str2 = "Hello, world!!!!!!!!!!!!";
x = strncmp(str1,str2,13);
y = strncmp(str1,str2,14);

Stateflow chart that uses the srtncmp operator in a state.

Input Arguments

expand all

Input strings, specified as string scalars. Enclose literal string 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 R2021b