Skip to Main Content Skip to Search
Product Documentation

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.

Tips

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

See Also

regexp | regexpi | strcmp | strcmpi | strfind | strncmp

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS