Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

strcmp, strcmpi - Compare strings

Syntax

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

Each of these syntaxes applies to both strcmp and strcmpi. The strcmp function is case sensitive in matching strings, while strcmpi is not.

Description

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

TF = strcmp('str1', 'str2') compares the 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 strcmp does not return true unless the sizes of both arrays are equal, and the contents of the two arrays are the same.

TF = strcmp('str', C) compares string str to the 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 = strcmp(C1, C2) 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 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 strcmp and strcmpi is not the same as the C language convention.

strcmp and strcmpi support international character sets.

Examples

Example 1

Perform a simple comparison of two strings:

strcmp('Yes', 'No')
ans =
     0
strcmp('Yes', 'Yes')
ans =
     1

Example 2

Create 3 cell arrays of strings:

A = {'MATLAB','SIMULINK';                      ...
     'Toolboxes', 'The MathWorks'};

B = {'Handle Graphics', 'Real Time Workshop';   ...
     'Toolboxes', 'The MathWorks'};

C = {'handle graphics', 'Signal Processing';   ...
     '  Toolboxes', 'The MATHWORKS'};

Compare cell arrays A and B with sensitivity to case:

strcmp(A, B)
ans =
     0     0
     1     1

Compare cell arrays B and C without sensitivity to case. Note that 'Toolboxes' doesn't match because of the leading space characters in C{2,1} that do not appear in B{2,1}:

strcmpi(B, C)
ans =
     1     0
     0     1

Example 3

Compare a string vector to a cell array of strings, a string vector to a string array, and a string array to a cell array of strings. Start by creating a cell array of strings (cellArr), a string array containing the same strings plus space characters for padding s(strArr), and a string vector containing one of the strings plus padding (strVec):

cellArr = { ...
   'There are 10 kinds of people in the world,'; ...
   'those who understand binary math,'; ... 
   'and those who don''t.'};

strArr = char(cellArr);
strVec = strArr(2,:)
strVec =
    those who understand binary math,

Remove the space padding from the string vector and compare it to the cell array. The MATLAB software compares the string with each row of the cell array, finding a match on the second row:

strcmp(deblank(strVec), cellArr)
ans =
     0
     1
     0

Compare the string vector with the string array. Unlike the case above, MATLAB does not compare the string vector with each row of the string array. It compares the entire contents of one against the entire contents of the other:

strcmp(strVec, strArr)
ans =
     0

Lastly, compare each row of the three-row string array against the same rows of the cell array. MATLAB finds them all to be equivalent. Note that in this case you do not have to remove the space padding from the string array:

strcmp(strArr, cellArr)
ans =
     1
     1
     1

See Also

strncmp, strncmpi, strmatch, strfind, findstr, regexp, regexpi, regexprep, regexptranslate

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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