strcmpi - Compare strings (case insensitive) - MATLAB (original) (raw)
Compare strings (case insensitive)
Syntax
Description
[tf](#bvjrpgt-tf) = strcmpi([s1,s2](#bvjrpgt-s1s2))
comparess1
and s2
, ignoring any differences in letter case, 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, aside from case. The return result tf
is of data typelogical
.
The input arguments can be any combination of string arrays, character vectors, and cell arrays of character vectors.
Examples
Compare two different character vectors, ignoring any differences in letter case.
s1 = 'Yes'; s2 = 'No'; tf = strcmpi(s1,s2)
strcmpi
returns 0
because s1
and s2
are not equal, even when ignoring case.
Compare two equal character vectors.
s1 = 'Yes'; s2 = 'yes'; tf = strcmpi(s1,s2)
strcmpi
returns 1
because s1
and s2
are equal when ignoring case.
Find text that matches the word 'once'
in a cell array of character vectors. Ignore case.
s1 = 'once'; s2 = {'Once','upon'; 'a','time'}; tf = strcmpi(s1,s2)
tf = 2×2 logical array
1 0 0 0
When you ignore case, there is one occurrence of s1
in the array s2
, and it occurs at the element s2(1,1)
.
Create two cell arrays of character vectors. To compare them while ignoring case, use the strcmpi
function.
s1 = {'Tinker', 'Tailor'; ' Soldier', 'Spy'}; s2 = {'Tinker', 'Baker'; 'Soldier', 'SPY'};
tf = strcmpi(s1,s2)
tf = 2×2 logical array
1 0 0 1
tf(1,1)
is 1
because 'Tinker'
is in the first cell of both arrays. tf(2,2)
is 1
because 'Spy'
and 'SPY'
differ only in case. tf(2,1)
is 0
because ' Soldier'
in s1(2,1)
has whitespace characters, and 'Soldier'
in s2(2,1)
does not.
Compare two string arrays, ignoring case, using strcmpi
.
s1 = ["A","bc"; "def","G"]; s2 = ["B","c"; "DEF","G"];
tf = strcmpi(s1,s2)
tf = 2×2 logical array
0 0 1 1
Input Arguments
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
True or false result, returned as a 1
or 0
of data type logical
.
- If each input is either a string scalar 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
strcmpi
function is intended for comparison of text. If used on an unsupported data type,strcmpi
always returns0
. - For case-sensitive text comparison, use
strcmp
instead ofstrcmpi
. - Although
strcmpi
shares a name with a C function, it does not follow the C language convention of returning0
when the text inputs match.
Extended Capabilities
Thestrcmpi
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- Enumeration inputs are not supported.
- Input values from the
char
class must be in the range 0-127. - 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
.
Version History
Introduced before R2006a