Documentation

strcmp

比较字符串

Description

example

tf= strcmp(s1,s2)comparess1s2并返回1(真的)如果两者是相同的,并且0(false) otherwise. Text is considered identical if the size and content of each are the same. The return resulttfis of data typelogical

输入参数可以是字符串数组,字符向量和字符向量的单元格数组的任何组合。

Examples

collapse all

Compare two different character vectors.

s1 ='Yes';S2 ='No';tf = strcmp(S1,S2)
tf =logical0

strcmpreturns0becauses1s2are not equal.

Compare two equal character vectors.

s1 ='Yes';S2 ='Yes';tf = strcmp(S1,S2)
tf =logical1

strcmpreturns1becauses1s2相等。

Find the word'upon'in a cell array of character vectors.

s1 ='upon';S2 = {'Once','upon';'a','time'};tf = strcmp(S1,S2)
tf =2x2逻辑数组0 1 0 0

There is only one occurrence ofs1在数组中s2, 和it occurs at elementS2(1,2)

比较每个元素在两个cell arrays of character vectors.

s1 = {'Time',“苍蝇”,'when';'you''re',“有”,'乐趣。'};S2 = {'Time','drags','when';'you''re','anxiously','等待。'};tf = strcmp(S1,S2)
tf =2x3 logical array1 0 1 1 0 0

There are three instances of equal elements ins1s2。These are'Time'at indices(1,1),'when'at indices(1,3), 和'you''re'at indices(2,1)

Starting in R2017a, you can create strings using double quotes. Compare string arrays usingstrcmp

s1 = [“一个”,"bc";“ def”,"G"]; s2 = [“ B”,“C”;“ def”,"G"]; tf = strcmp(s1,s2)
tf =2x2逻辑数组0 0 1 1

You can compare and sort string arrays with relational operators, just as you can with numeric arrays.

利用==确定两个字符串阵列的元素相等。

s1 == s2
ans =2x2逻辑数组0 0 1 1

利用<to determine which elements ofs1are less than the corresponding elements ofs2according to ASCII dictionary order.

S1 
                   
ans =2x2逻辑数组1 1 0 0

Input Arguments

collapse all

输入文本,每个输入指定为字符向量,字符数组,字符向量的单元格数组或字符串数​​组。输入的顺序不影响比较结果。

  • If boths1s2are string arrays or cell arrays of character vectors, thens1s2must be the same size, unless one of them is scalar.

  • If boths1s2are character arrays with multiple rows, thens1s2can 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

是的,或错误的结果,返回为1或者0数据类型logical

  • 如果每个输入是字符串标量,标量单元格或字符向量,则tfis a scalar.

  • 如果至少一个输入是字符串数组或字符向量的单元格数组,则tf与输入阵列相同的数组大小。

  • If one input is a character array with multiple rows, and the other input is either a scalar cell or a string scalar, thentfis ann-经过-1array, wherenis the number of rows in the character array.

  • If both inputs are character arrays,tfis a scalar.

提示

  • Thestrcmpfunction is intended for comparison of text. If used on unsupported data types,strcmp总是返回0

  • For case-insensitive text comparison, usestrcmpiinstead ofstrcmp

  • Althoughstrcmpshares a name with a C function, it does not follow the C language convention of returning0when the text inputs match.

  • With string arrays, you can use relational operators (==,〜=,<,>,<=,> =) instead ofstrcmp。您可以像数字阵列一样比较和对字符串数组进行比较。

Extended Capabilities

在R2006a之前引入