This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Compare first n characters of strings (case sensitive)


TF = strncmp(str,str,n)
TF = strncmp(str,C,n)
TF = strncmp(C,C,n)


TF = strncmp(str,str,n) compares the first n characters of two character vectors for equality. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.

TF = strncmp(str,C,n) compares the first n characters of a character vector with the first n characters of each element of a cell array of character vectors. The function returns a logical array the same size as the C input in which logical 1 represents equality. The order of the first two input arguments is not important.

TF = strncmp(C,C,n) compares each element of one cell array of character vectors with the same element of the other. strncmp attempts to match only the first n characters of these character vectors. The function returns a logical array the same size as either input array.

Input Arguments


A character vector or m-by-n character array.


Cell array of character vectors.


Maximum number of characters to compare. Must be a scalar, integer-valued double.

Output Arguments


When both inputs are character arrays, TF is a scalar logical value. This value is logical 1 (true) if the size and content of both arrays are equal, and logical 0 (false) if they are not.

When either or both inputs are a cell array of character vectors, TF is an array of logical ones and zeros. This array is the same size as the input cell array(s), and contains logical 1 (true) for those elements of the input arrays that are a match, and logical 0 (false) for those elements that are not.


Before trying the strncmp function, use strcmp to perform a simple comparison of the two input character vectors. Because only the first 13 characters are the same, strcmp returns logical 0:

strcmp('Kansas City, KS', 'Kansas City, MO')
ans =

Do the comparison again, but this time using strncmp and specifying the number of characters to compare:

chars2compare = length('Kansas City, KS') - 2
ans =
strncmp('Kansas City, KS', 'Kansas City, MO', chars2compare)
ans =

From a list of 10 MATLAB® functions, find those that apply to using a camera:

function_list = {'calendar' 'case' 'camdolly' 'circshift' ...
                 'caxis' 'camtarget' 'cast' 'camorbit' ...
                 'callib' 'cart2sph'};

strncmp(function_list, 'cam', 3)
ans =
    0    0    1    0    0    1    0    1    0    0

function_list{strncmp(function_list, 'cam', 3)}
ans =
ans =
ans =

Create two 5-by-10 character arrays str1 and str2 that are equal, except for the element at row 4, column 3. Using linear indexing, this is element 14:

str1 =

str2 = str1;
str2(4,3) = '-'
str2 =

Because MATLAB compares the arrays in linear order (that is, column by column rather than row by row), strncmp finds only the first 13 elements to be the same:

str1   A B C D E A B C D E A B C D E
str2   A B C D E A B C D E A B C - E
                             element 14

strncmp(str1, str2, 13)
ans =

strncmp(str1, str2, 14)
ans =

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.


  • The strncmp function is intended for comparison of character data. When used to compare numeric data, it returns logical 0.

  • Use strncmpi for case-insensitive text comparisons.

  • Any leading and trailing blanks in either of the character vectors are explicitly included in the comparison.

  • The value returned by strncmp is not the same as the C language convention.

  • strncmp supports international character sets.

Introduced before R2006a

Was this topic helpful?