This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Compare first n characters of strings (case insensitive)


tf = strncmpi(s1,s2,n)



tf = strncmpi(s1,s2,n) compares the first n characters of s1 and s2, ignoring any differences in letter case. The function 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, up to the first n characters of each piece of text, ignoring case. The return result tf is of data type logical.

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


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 = logical

tf is 1 because s1 starts with 'DATA', and s2 starts with 'data'.

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

s1 = ["Jacques";
s2 = "JEAN";

tf = strncmpi(s1,s2,4)
tf = 5×1 logical array

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

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 both s1 and s2 are string arrays or cell arrays of character vectors, then s1 and s2 must be the same size, unless one of them is scalar.

  • If both s1 and s2 are character arrays with multiple rows, then s1 and s2 can 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

Number of characters to compare, specified as an integer.

  • If n is 0, then strncmpi always returns 1. By convention, the zeroth character of a character vector or a string scalar is always '', a 0-by-0 character array.

  • If n is less than 0, then strncmpi treats it as 0.

  • If n is greater than the length of the shortest input text, then strncmpi returns 0.

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

Output Arguments

collapse all

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

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

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

  • If both inputs are character arrays, tf is a scalar.


  • The strncmpi function is intended for comparison of text. If used on numeric arrays, strncmpi always returns 0.

  • For case-sensitive text comparison, use strncmp instead of strncmpi.

  • Although strncmpi shares a name with a C function, it does not follow the C language convention of returning 0 when the text inputs match.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?