Accelerating the pace of engineering and science

# strcmp

Compare strings

## Description

example

tf = strcmp(s1,s2) compares strings s1 and s2 and returns 1 (true) if the two are identical. Otherwise, strcmp returns 0 (false). Strings are 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 single strings, strings in scalar cell arrays, character arrays, and cell arrays of strings.

## Examples

expand all

### Compare Two Strings

Compare two different strings.

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

0```

strcmp returns 0 because the two strings are not equal.

Compare two equal strings.

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

1
```

strcmp returns 1 because the two strings are equal.

### Compare String and Cell Array of Strings

Compare a string, 'upon', to each element of a cell array of strings.

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

0     1
0     0
```

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

### Compare Two Cell Arrays of Strings

Compare each element in a cell array of strings to the corresponding element in a second cell array of strings.

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

1     0     1
1     0     0```

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

## Input Arguments

expand all

### s1,s2 — Input stringscharacter array | cell array of strings

Input strings, specified as a cell array of strings, or character array with one or more rows. The order of the inputs does not affect the comparison results.

• If both s1,s2 are nonscalar cell arrays, then s1,s2 must be the same size.

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

• When comparing a nonscalar cell array 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

## Output Arguments

expand all

### tf — True or false1 | 0 | logical array

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

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

• If at least one input is a cell array of strings, tf is an array the same size as the cell array.

• If s1 and s2 are a string in a scalar cell and a character array with multiple rows, then tf is an n-by-1 array, where n is the number of rows in the character array.

expand all

### Tips

• Use the strcmp function for comparison of strings. If you use strcmp on numeric arrays, it always returns 0.

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

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