strcmp - Compare strings - MATLAB (original) (raw)
Syntax
Description
[tf](#btwfvmr-tf) = strcmp([s1,s2](#btwfvmr-s1s2))
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
.
The input arguments can be any combination of string arrays, character vectors, and cell arrays of character vectors.
Examples
Compare Two Character Vectors
Compare two different character vectors.
s1 = 'Yes'; s2 = 'No'; tf = strcmp(s1,s2)
strcmp
returns 0
because s1
and s2
are not equal.
Compare two equal character vectors.
s1 = 'Yes'; s2 = 'Yes'; tf = strcmp(s1,s2)
strcmp
returns 1
because s1
and s2
are equal.
Find Text in Cell Array
Find the word 'upon'
in a cell array of character vectors.
s1 = 'upon'; s2 = {'Once','upon'; 'a','time'}; tf = strcmp(s1,s2)
tf = 2x2 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 Two Cell Arrays of Character Vectors
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 = 2x3 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)
.
Compare String Arrays
Compare string arrays using strcmp
.
s1 = ["A","bc"; "def","G"]; s2 = ["B","c"; "def","G"];
tf = strcmp(s1,s2)
tf = 2x2 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.
ans = 2x2 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.
ans = 2x2 logical array
1 1 0 0
Input Arguments
s1,s2
— Input text
character vector | character array | cell array of character vectors | string array
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
ands2
are string arrays or cell arrays of character vectors, thens1
ands2
must be the same size, unless one of them is scalar. - If both
s1
ands2
are character arrays with multiple rows, thens1
ands2
can have different numbers of rows. - When comparing a nonscalar cell array of character vectors or a string array to a multirow character array, the cell array or string array must be a column vector with the same number of rows as the character array.
Data Types: char
| cell
| string
Output Arguments
tf
— True or false result
1
| 0
| logical array
True or false result, returned as a 1
or 0
of data type logical
.
- If each input is either a string scalar, scalar cell, 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 ann
-by-1
array, wheren
is the number of rows in the character array. - If both inputs are character arrays,
tf
is a scalar.
Tips
- The
strcmp
function is intended for comparison of text. If used on unsupported data types,strcmp
always returns0
. - For case-insensitive text comparison, use
strcmpi
instead ofstrcmp
. - Although
strcmp
shares a name with a C function, it does not follow the C language convention of returning0
when the text inputs match. - With string arrays, you can use relational operators (
==
,~=
,<
,>
,<=
,>=
) instead ofstrcmp
. You can compare and sort string arrays just as you can with numeric arrays.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Thestrcmp
function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- Enumeration inputs are not supported.
- When one input is a cell array and the other input is a character array, the character array must be a compile-time row vector.
- When both inputs are empty character arrays that have different sizes, the generated code returns
true
.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
- Inputs must be string arrays or cell arrays of character vectors.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a