Documentation Center |
Compare strings (case sensitive)
TF = strcmp(string,string)
TF = strcmp(string,cellstr)
TF = strcmp(cellstr,cellstr)
TF = strcmp(string,string) compares two strings for equality. The strings are considered to be equal if the size and content of each are the same. The function returns a scalar logical 1 for equality, or scalar logical 0 for inequality.
TF = strcmp(string,cellstr) compares a string with each element of a cell array of strings. The function returns a logical array the same size as the cellstr input in which logical 1 represents equality. The order of the input arguments is not important.
TF = strcmp(cellstr,cellstr) compares each element of one cell array of strings with the same element of the other. The function returns a logical array the same size as either cell array input.
string |
A single character string or n-by-1 array of strings. |
cellstr |
A cell array of strings. |
Perform a simple comparison of two strings:
strcmp('Yes', 'No')
ans =
0
strcmp('Yes', 'Yes')
ans =
1Create two cell arrays of strings and call strcmp to compare them:
A = {'Handle Graphics', 'Statistics'; ...
' Toolboxes', 'MathWorks'};
B = {'Handle Graphics', 'Signal Processing'; ...
'Toolboxes', 'MATHWORKS'};
match = strcmp(A, B)
match =
1 0
0 0The result of comparing the two cell arrays is:
match{1,1} is 1 because "Handle Graphics" in A{1,1} matches the same text in B{1,1}.
match{1,2} is 0 because "Statistics" in A{1,2} does not match "Signal Processing" in B{1,2}.
match{2,1} is 0 because " Toolboxes", in A{2,1} contains leading space characters that are not in B{2,1}.
match{2,2} is 0 because "MathWorks" in A{2,2} uses different letter case than "MATHWORKS" in B{2,2}, and strcmp does a case-sensitive comparison.
The following example has three parts. It compares
A string to an array of strings.
A padded string to a cell array of strings.
An unpadded string to a cell array of strings.
Start by creating the necessary data structures.
Cell array of strings –
Create a 3-element cell array of strings:
cellarr = { ...
'There are 10 kinds of people in the world,'; ...
'those who understand binary math,'; ...
'and those who don''t.'};String array –
From the cell array, create a string array. The string array contains space characters at the end of rows 2 and 3 for the padding needed to make the array rectangular:
strarr = char(cellarr) strarr = There are 10 kinds of people in the world, those who understand binary math, and those who don't. % Each line ends here ^
String vector –
From row 2 of the string array, create a string vector. This string is also padded with spaces at the end:
strvec = strarr(2,:) strvec = those who understand binary math, % Padded line ends here ^
Begin the comparisons. Start by comparing the string vector with the string array. When comparing character arrays, strcmp does not do a row-by-row comparison. It compares all of the 1-by-42 strvec with all of the 3-by-42 strarr. Finding them to be different, the answer is false and strcmp returns logical 0:
strcmp(strvec, strarr)
ans =
0
Compare the string vector to the cell array. Even though strvec is essentially the same as row 2 of cellarr, it is not a match because of the space padding in strvec:
strcmp(strvec, cellarr)
ans =
0
0
0
Remove the space padding from the string vector and compare it to the cell array. strcmp compares strvec with each row of the cellarr, finding a match with the second row of the latter:
strcmp(deblank(strvec), cellarr)
ans =
0
1
0