This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Compare first n characters of strings (case insensitive)


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


TF = strncmpi(str,str,n) compares the first n characters of two character vectors for equality, ignoring any differences in letter case. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.

TF = strncmpi(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, ignoring letter case. The function returns a logical array the same size as the C input in which logical 1 represents equality. The order of the input arguments is not important.

TF = strncmpi(C,C,n) compares each element of one cell array of character vectors with the same element of the other, ignoring letter case. strncmpi 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.


A 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.


From a list of 10 MATLAB® functions, find those that apply to using a camera. Do the comparison without sensitivity to letter case:

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

strncmpi(function_list, 'CAM', 3)
ans =
    0    0    1    0    0    1    0    1    0    0

function_list{strncmpi(function_list, 'CAM', 3)}
ans =
ans =
ans =

More About

collapse all

Tall Array Support

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


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

  • Use strncmp for case-sensitive text comparisons.

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

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

  • strncmpi supports international character sets.

Introduced before R2006a

Was this topic helpful?