strcmpi

Compare strings (case insensitive)

Description

example

tf = strcmpi(s1,s2) compares s1 and s2, ignoring any differences in letter case. The function 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 type logical.

The input arguments can be any combination of string arrays, character vectors, and cell arrays of character vectors.

Examples

collapse all

Compare two different character vectors, ignoring any differences in letter case.

s1 = 'Yes';
s2 = 'No';
tf = strcmpi(s1,s2)
tf = logical
0

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)
tf = logical
1

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 = 2x2 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 = 2x2 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.

Starting in R2017a, you can create strings using double quotes. Compare two string arrays, ignoring case, using strcmpi.

s1 = ["A","bc";
"def","G"];
s2 = ["B","c";
"DEF","G"];

tf = strcmpi(s1,s2)
tf = 2x2 logical array

0   0
1   1

Input Arguments

collapse all

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 and s2 are string arrays or cell arrays of character vectors, then s1 and s2 must be the same size, unless one of them is scalar.

• If both s1 and s2 are character arrays with multiple rows, then s1 and s2 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

collapse all

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 an n-by-1 array, where n 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 returns 0.

• For case-sensitive text comparison, use strcmp instead of strcmpi.

• Although strcmpi shares a name with a C function, it does not follow the C language convention of returning 0 when the text inputs match.