strcmpi

Compare strings (case insensitive)

Syntax


TF = strcmpi(string,string)
TF = strcmpi(string,cellstr)
TF = strcmpi(cellstr,cellstr)

Description

TF = strcmpi(string,string) compares two strings for equality, ignoring any differences in letter case. 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 = strcmpi(string,cellstr) compares a string with each element of a cell array of strings, ignoring letter case. 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 = strcmpi(cellstr,cellstr) compares each element of one cell array of strings with the same element of the other, ignoring letter case. The function returns a logical array the same size as the input arrays.

Input Arguments

string

A single character string or n-by-1 array of strings.

cellstr

A cell array of strings.

Output Arguments

TF

When both inputs are character arrays, TF is a scalar logical value. This value is logical 1 (true) if the size and content of both arrays are equal, and logical 0 (false) if they are not.

When either or both inputs are a cell array of strings, TF is an array of logical ones and zeros. This array is the same size as the input cell array(s), and contains logical 1 (true) for those elements of the input arrays that are a match, and logical 0 (false) for those elements that are not.

Examples

Perform a simple case-insensitive comparison of two strings:

strcmpi('Yes', 'No')
ans =
     0
strcmpi('Yes', 'yes')
ans =
     1

Create two cell arrays of strings and call strcmpi to compare them:

A = {'Handle Graphics', 'Statistics';   ...
     '  Toolboxes', 'MathWorks'};

B = {'Handle Graphics', 'Signal Processing';    ...
     'Toolboxes', 'MATHWORKS'};
match = strcmpi(A, B)
match =
     1     0
     0     1

The 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 1 because even though "MathWorks" in A{2,2} uses different letter case than "MATHWORKS" in B{2,2}, strcmpi performs the comparison without sensitivity to letter case.

More About

expand all

Tips

  • The strcmpi function is intended for comparison of character data. When used to compare numeric data, it returns logical 0.

  • Use strcmp for case-sensitive string comparisons.

  • Any leading and trailing blanks in either of the strings are explicitly included in the comparison.

  • The value returned by strcmpi is not the same as the C language convention.

  • strcmpi supports international character sets.

Was this topic helpful?