strncmp, strncmpi - Compare first n characters of strings

Syntax

TF = strncmp('str1', 'str2', n)
TF = strncmp('str', C, n)
TF = strncmp(C1, C2, n)

Each of these syntaxes apply to both strncmp and strncmpi. The strncmp function is case sensitive in matching strings, while strncmpi is not.

Description

Although the following descriptions show only strncmp, they apply to strncmpi as well. The two functions are the same except that strncmpi compares strings without sensitivity to letter case:

TF = strncmp('str1', 'str2', n) compares the first n characters of strings str1 and str2 and returns logical 1 (true) if they are identical, and returns logical 0 (false) otherwise. str1 and str2 can be character arrays of any dimension, but strncmp does not return true unless the sizes of both arrays are equal, and the contents of the two arrays are the same.

TF = strncmp('str', C, n) compares the first n characters of str to the first n characters of each element of cell array C, where str is a character vector (or a 1-by-1 cell array), and C is a cell array of strings. The function returns TF, a logical array that is the same size as C and contains logical 1 (true) for those elements of C that are a match, and logical 0 (false) for those elements that are not. The order of the first two input arguments is not important.

TF = strncmp(C1, C2, n) compares each element of C1 to the same element in C2, where C1 and C2 are equal-size cell arrays of strings. Input C1 or C2 can also be a character array with the right number of rows. The function attempts to match only the first n characters of each string. The function returns TF, a logical array that is the same size as C1 and C2, and contains logical 1 (true) for those elements of C1 and C2 that are a match, and logical 0 (false) for those elements that are not.

Remarks

These functions are intended for comparison of character data. When used to compare numeric data, they return logical 0.

Any leading and trailing blanks in either of the strings are explicitly included in the comparison.

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

strncmp and strncmpi support international character sets.

Examples

Example 1

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

Example 2

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

str1 = ['AAAAAAAAAA'; 'BBBBBBBBBB'; 'CCCCCCCCCC'; ...
        'DDDDDDDDDD'; 'EEEEEEEEEE']
str1 =
    AAAAAAAAAA
    BBBBBBBBBB
    CCCCCCCCCC
    DDDDDDDDDD
    EEEEEEEEEE

str2 = str1;
str2(4,3) = '-'
str2 =
    AAAAAAAAAA
    BBBBBBBBBB
    CCCCCCCCCC
    DD-DDDDDDD
    EEEEEEEEEE

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 =
     1

strncmp(str1, str2, 14)
ans =
     0

See Also

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

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS