Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

strcmp

Compare strings

Syntax

Description

example

tf = strcmp(s1,s2) compares s1 and s2 and returns 1 (true) if the two are identical and 0 (false) otherwise. Text is considered identical if the size and content of each are the same. The return result tf is of data type logical.

Inputs can be combinations of string arrays, character vectors, and cell arrays of character vectors.

Examples

collapse all

Compare two different character vectors.

s1 = 'Yes';
s2 = 'No';
tf = strcmp(s1,s2)
tf =

  logical

   0

strcmp returns 0 because s1 and s2 are not equal.

Compare two equal character vectors.

s1 = 'Yes';
s2 = 'Yes';
tf = strcmp(s1,s2)
tf =

  logical

   1

strcmp returns 1 because s1 and s2 are equal.

Find the word 'upon' in a cell array of character vectors.

s1 = 'upon';
s2 = {'Once','upon';
      'a','time'};
tf = strcmp(s1,s2)
tf =

  2×2 logical array

   0   1
   0   0

There is only one occurrence of s1 in array s2, and it occurs at element s2(1,2).

Compare each element in two cell arrays of character vectors.

s1 = {'Time','flies','when';
      'you''re','having','fun.'};
s2 = {'Time','drags','when';
      'you''re','anxiously','waiting.'};
tf = strcmp(s1,s2)
tf =

  2×3 logical array

   1   0   1
   1   0   0

There are three instances of equal elements in s1 and s2. These are 'Time' at indices (1,1), 'when' at indices|(1,3)|, and 'you''re' at indices (2,1).

Starting in R2016b, you can create string arrays using the string function, and compare them using strcmp.

s1 = string({'A', 'bc';
             'def', 'G'});
s2 = string({'B', 'c';
             'def', 'G'});

tf = strcmp(s1,s2)
tf =

  2×2 logical array

   0   0
   1   1

You can compare and sort string arrays with relational operators, just as you can with numeric arrays.

Use == to determine which elements of two string arrays are equal.

s1 == s2
ans =

  2×2 logical array

   0   0
   1   1

Use < to determine which elements of s1 are less than the corresponding elements of s2 according to ASCII dictionary order.

s1 < s2
ans =

  2×2 logical array

   1   1
   0   0

Text processing functions (such as strfind and regexp) accept string arrays as inputs, but other functions (for example, addpath) do not.

Input Arguments

collapse all

Input text, with each input specified as a character vector, a character array, a cell array of character vectors, or a string array. The order of the inputs does not affect the comparison results.

  • If both s1,s2 are string arrays or cell arrays of character vectors, then s1,s2 must be the same size.

  • If both s1,s2 are character arrays with multiple rows, then s1,s2 can have different numbers of rows.

  • When comparing a nonscalar cell array of character vectors and a multirow character array, the cell array must be a column vector with the same number of rows as the character array.

Data Types: char | cell | string

Output Arguments

collapse all

True or false result, returned as a 1 or 0 of data type logical.

  • If each input is either a string scalar or a character vector, then tf is a scalar.

  • If at least one input is either a string array or a cell array of character vectors, then tf is an array the same size as the input array.

  • If one input is a character array with multiple rows, and the other input is either a scalar cell or a string scalar, then tf is an n-by-1 array, where n is the number of rows in the character array.

  • If both inputs are character arrays, tf is a scalar.

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Tips

  • The strcmp function is intended for comparison of text. If used on numeric arrays, strcmp always returns 0.

  • For case-insensitive text comparison, use strcmpi instead of strcmp.

  • The value that strcmp returns is not the same as the C language convention.

  • With string arrays, you can use relational operators (==, ~=, <, >, <=, >=) instead of strcmp. You can compare and sort string arrays just as you can with numeric arrays.

See Also

| | | | | | | |

Introduced before R2006a

Was this topic helpful?