MATLAB Function Reference    

Compare the first n characters of two strings



k = strncmp('str1','str2',n) returns logical true (1) if the first n characters of the strings str1 and str2 are the same, and returns logical false (0) otherwise. Arguments str1 and str2 may also be cell arrays of strings.

TF = strncmp(S,T,N) where either S or T is a cell array of strings, returns an array TF the same size as S and T containing 1 for those elements of S and T that match (up to n characters), and 0 otherwise. S and T must be the same size (or one can be a scalar cell). Either one can also be a character array with the right number of rows.


The command strncmp is case sensitive. Any leading and trailing blanks in either of the strings are explicitly included in the comparison.

strncmp is intended for comparison of character data. When used to compare numeric data, strncmp returns 0.

See Also

strncmpi, strcmp, strcmpi, strmatch, strfind, findstr, regexp, regexpi, regexprep

  strmatch strncmpi