strcmp

Compares text for exact equality, case sensitive.

Category: Language Fundamentals > Data Types

Syntax

tf = strcmp(s1, s2)

Returns true if the two texts are identical.

Description

Two texts match only when they have the same length and the same characters, including case and spaces. Inputs that are not text, such as numbers, never match, and strcmp simply returns false for them instead of raising an error.

Using == on character vectors compares code by code and fails when the lengths differ, so use strcmp to test text equality.

Input arguments

s1, s2
Character vectors, strings, cell arrays of character vectors, or string arrays. Arrays are compared element by element, and a scalar is compared with every element.

Output arguments

tf
Logical scalar, or a logical array the size of the cell or string array.

Example

Equal, different case and cells

strcmp('abc', 'abc')
strcmp('abc', 'ABC')
strcmp({'a','b','a'}, 'a')
strcmp(1, 'a')
Output
ans = logical
   1
ans = logical
   0
ans = 1×3 logical array

   1   0   1
ans = logical
   0

See also

strcmpi strncmp strncmpi isequal contains