Documentation Center

  • Trials
  • Product Updates

strncmpi

Compare first n characters of strings (case insensitive)

Syntax

TF = strncmpi(string,string,n)
TF = strncmpi(string,cellstr,n)
TF = strncmpi(cellstr,cellstr,n)

Description

TF = strncmpi(string,string,n) compares the first n characters of two strings 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(string,cellstr,n) compares the first n characters of a string with the first n characters of each element of a cell array of strings, ignoring letter case. The function returns a logical array the same size as the cellstr input in which logical 1 represents equality. The order of the input arguments is not important.

TF = strncmpi(cellstr,cellstr,n) compares each element of one cell array of strings with the same element of the other, ignoring letter case. strncmpi attempts to match only the first n characters of these strings. The function returns a logical array the same size as either input array.

Input Arguments

string

A single character string or n-by-1 array of strings.

cellstr

A cell array of strings.

n

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

Output Arguments

TF

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 strings, 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.

Examples

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 =
   camdolly
ans =
   Camtarget
ans =
   camorbit

More About

expand all

Tips

  • 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 string comparisons.

  • Any leading and trailing blanks in either of the strings 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.

See Also

| | | | |

Was this topic helpful?