Documentation

strncmpi

Compare firstncharacters of strings (case insensitive)

Description

example

tf= strncmpi(s1,s2,n)compares up toncharacters ofs1ands2, ignoring any differences in letter case. The function returns1(true) if the two are identical and0(false) otherwise. Text is considered identical if the content of each is the same up to the end or the firstncharacters, whichever comes first, ignoring case. The return resulttfis of data typelogical.

前两个输入参数可以是任何combination of string arrays, character vectors, and cell arrays of character vectors.

Examples

collapse all

Create two character vectors. Compare the first four characters of each, ignoring case.

s1 ='DATA.TAR.GZ'; s2 ='data-samples.xls'; tf = strncmpi(s1,s2,4)
tf =logical1

tfis1becauses1starts with'DATA', ands2starts with'data'.

Starting in R2017a, you can create strings using double quotes. Create a string array that contains names. Find the names that start with'JEAN', ignoring case.

s1 = ["Jacques";"Jean";"Jeanne";"Jean-Luc";"Julie"]; s2 ="JEAN"; tf = strncmpi(s1,s2,4)
tf =5x1 logical array0 1 1 1 0

tfis1for all names whose first four characters match'JEAN'when you ignore case.

Alternatively, you can use thestartsWithfunction.

tf = startsWith(s1,s2,'IgnoreCase',真正的)
tf =5x1 logical array0 1 1 1 0

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 boths1ands2are string arrays or cell arrays of character vectors, thens1ands2must be the same size, unless one of them is scalar.

  • If boths1ands2are character arrays with multiple rows, thens1ands2can have different numbers of rows.

  • When comparing a nonscalar cell array of character vectors or 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

Maximum number of characters to compare, specified as an integer.

  • Ifnis0, thenstrncmpialways returns1. By convention, the zeroth character of a character vector or a string scalar is always'', a0-by-0character array.

  • Ifnis less than0, thenstrncmpitreats it as0.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output Arguments

collapse all

True or false result, returned as a1or0of data typelogical.

  • If each input is either a string scalar or a character vector, thentfis a scalar.

  • If at least one input is either a string array or a cell array of character vectors, thentfis 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, thentfis ann-by-1array, wherenis the number of rows in the character array.

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

Tips

  • Thestrncmpifunction is intended for comparison of text. If used on numeric arrays,strncmpialways returns0.

  • For case-sensitive text comparison, usestrncmpinstead ofstrncmpi.

  • Althoughstrncmpi与C共享名称function, it does not follow the C language convention of returning0when the text inputs match.

Extended Capabilities

Introduced before R2006a