Skip to Main Content Skip to Search
Product Documentation

strcmp - Compare strings (case sensitive)

Syntax

TF = strcmp(string,string)
TF = strcmp(string,cellstr)
TF = strcmp(cellstr,cellstr)

Description

TF = strcmp(string,string) compares two strings for equality. The strings are considered to be equal if the size and content of each are the same. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.

TF = strcmp(string,cellstr) compares a string with each element of a cell array of strings. 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 = strcmp(cellstr,cellstr) compares each element of one cell array of strings with the same element of the other. The function returns a logical array the same size as either cell array input.

Tips

Input Arguments

string

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

cellstr

A cell array of strings.

Output Arguments

TF

When both inputs are character arrays, TF is a scalar logical 1 or 0. This value is logical 1 (true) if the size and content of both arrays are equal, and logical 0 (false) if it is not.

When either or both inputs is 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

Perform a simple comparison of two strings:

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

Create two cell arrays of strings and call strcmp to compare them:

A = {'Handle Graphics', 'Statistics';   ...
     '  Toolboxes', 'MathWorks'};

B = {'Handle Graphics', 'Signal Processing';    ...
     'Toolboxes', 'MATHWORKS'};
match = strcmp(A, B)
match =
     1     0
     0     0

The result of comparing the two cell arrays is:

 

The following example has three parts. It compares

Start by creating the necessary data structures.

  1. Cell array of strings –

    Create a 3-element cell array of strings:

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

    From the cell array, create a string array. The string array contains space characters at the end of rows 2 and 3 for the padding needed to make the array rectangular:

    strarr = char(cellarr)
    strarr =
    There are 10 kinds of people in the world,
    those who understand binary math,         
    and those who don't.                      
    %                   Each line ends here   ^
    
  3. String vector –

    From row 2 of the string array, create a string vector. This string is also padded with spaces at the end:

    strvec = strarr(2,:)
    strvec =
    those who understand binary math,         
    %                 Padded line ends here   ^

Begin the comparisons. Start by comparing the string vector with the string array. When comparing character arrays, strcmp does not do a row-by-row comparison. It compares all of the 1-by-42 strvec with all of the 3-by-42 strarr. Finding them to be different, the answer is false and strcmp returns logical 0:

strcmp(strvec, strarr)
ans =
     0

Compare the string vector to the cell array. Even though strvec is essentially the same as row 2 of cellarr, it is not a match because of the space padding in strvec:

strcmp(strvec, cellarr)
ans =
     0
     0
     0

Remove the space padding from the string vector and compare it to the cell array. strcmp compares strvec with each row of the cellarr, finding a match with the second row of the latter:

strcmp(deblank(strvec), cellarr)
ans =
     0
     1
     0

See Also

regexp | regexpi | strcmpi | strfind | strncmp | strncmpi

  


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