Documentation Center

  • Trial Software
  • Product Updates

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.

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:

  • match{1,1} is 1 because "Handle Graphics" in A{1,1} matches the same text in B{1,1}.

  • match{1,2} is 0 because "Statistics" in A{1,2} does not match "Signal Processing" in B{1,2}.

  • match{2,1} is 0 because "  Toolboxes", in A{2,1} contains leading space characters that are not in B{2,1}.

  • match{2,2} is 0 because "MathWorks" in A{2,2} uses different letter case than "MATHWORKS" in B{2,2}, and strcmp does a case-sensitive comparison.

 

The following example has three parts. It compares

  • A string to an array of strings.

  • A padded string to a cell array of strings.

  • An unpadded string to a cell array of strings.

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

More About

expand all

Tips

  • The strcmp function is intended for comparison of character data. When used to compare numeric data, it returns logical 0.

  • Use strcmpi for case-insensitive string comparisons.

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

  • The value returned by strcmp is not the same as the C language convention.

  • strcmp supports international character sets.

See Also

| | | | |

Was this topic helpful?