| MATLAB Function Reference | Search  Help Desk |
| strcmp | Examples See Also |
Syntax
k = strcmp('str1','str2')
TF = strcmp(S,T)
Description
k = strcmp(str1,str2)
compares the strings str1 and str2 and returns logical true (1) if the two are identical, and logical false (0) otherwise.
TF = strcmp(S,T)
where either S or T is a cell array of strings, returns an array TF the same size as S and T containing 1 for those elements of S and T that match, and 0 otherwise. S and T must be the same size (or one can be a scalar cell). Either one can also be a character array with the right number of rows.
Remarks
Thestrcmp function is case sensitive.
When comparing a string array to a cell or cell array, the string array is deblanked (trailing spaces are removed) before comparison.
However, when comparing a scalar string (a string array with
only one row) to a cell or cell array, the scalar string is not deblanked.
Use deblank to eliminate any trailing blanks in the scalar string before
comparison.
Examples
These examples show the comparison of two strings:strcmp('Yes','No')
ans =
0
strcmp('Yes ','Yes')
ans =
0
This example compares a string to a cell array of strings:
A = {'MATLAB';'Simulink';'The MathWorks'}
A =
'MATLAB'
'Simulink'
'The MathWorks'
strcmp('The MathWorks',A)
ans =
0
0
1
These examples compare two cell arrays of strings:
A = {'MATLAB ';'Simulink ';'The MathWorks'};
B = {'MATLAB';'Stateflow' ;'The MathWorks'};
strcmp(A,B)
ans =
0
0
1
strcmp({'Simulink'}, B)
ans =
0
0
0
These examples demonstrate scalar expansion:
strcmp('hello', {'hello','world'})
ans =
1 0
strcmp({'hello'}, ['hello';'world'])
ans =
1
0
strcmp({'hello'}, ['hello '; 'world '])
ans =
1
0
This example compares a scalar string to a cell:
strcmp('hello ', {'hello','world'})
ans =
0 0
strcmp(deblank('hello '), {'hello','world'})
ans =
1 0
See Also
findstr, strcmpi, strmatch, strncmp