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.

example

Examples

collapse all

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

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.

Data Types: char | cell | string

Output Arguments

collapse all

True or false result, returned as a 1 or 0 of data type logical.

Tips

Extended Capabilities

expand all

Thestrcmpi function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

Version History

Introduced before R2006a