strncmpi - Compare first n characters of strings

(case insensitive) - MATLAB (original) (raw)

Compare first n characters of strings (case insensitive)

Syntax

Description

[tf](#bvjtblc-1-tf) = strncmpi([s1,s2](#bvjtblc-1-s1s2),[n](#bvjtblc-1-n)) compares up to n characters of s1 ands2, ignoring any differences in letter case, and returnstrue(1) if the two are identical andfalse(0) otherwise. Text is considered identical if the content of each is the same up to the end or the firstn characters, whichever comes first, ignoring case. The return result tf is of data typelogical.

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

example

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 is 1 because s1 starts with 'DATA', and s2 starts with 'data'.

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 = 5×1 logical array

0 1 1 1 0

tf is 1 for all names whose first four characters match 'JEAN' when you ignore case.

Alternatively, you can use the startsWith function.

tf = startsWith(s1,s2,'IgnoreCase',true)

tf = 5×1 logical array

0 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.

Data Types: char | cell | string

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

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

Output Arguments

collapse all

True or false result, returned as true(1) orfalse(0) or as alogical array.

Tips

Extended Capabilities

expand all

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

Usage notes and limitations:

Version History

Introduced before R2006a