strncmp - Compare first n characters of strings
(case sensitive) - MATLAB (original) (raw)
Compare first n
characters of strings (case sensitive)
Syntax
Description
[tf](#bvjs7kt-1-tf) = strncmp([s1,s2](#bvjs7kt-1-s1s2),[n](#bvjs7kt-1-n))
compares up to n
characters of s1
ands2
. The function returns 1
(true
) if the two are identical and 0
(false
) otherwise. Text is considered identical if the content of each is the same up to the end or the first n
characters, whichever comes first. 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.
Examples
Create two different character vectors. Compare the first 11 characters of them.
s1 = 'Kansas City, KS'; s2 = 'Kansas City, MO'; tf = strncmp(s1,s2,11)
tf
is 1
because both character vectors start with 'Kansas City'
.
Compare the two character vectors using strcmp
.
tf
is 0
because s1
and s2
end with different characters.
Create a string array that contains names. Find the names that start with 'Jean'
.
s1 = ["Jacques"; "Jean"; "Jeanne"; "Jean-Luc"; "Julie"]; s2 = "Jean";
tf = strncmp(s1,s2,4)
tf = 5×1 logical array
0 1 1 1 0
tf
is 1
for all names whose first four characters are 'Jean'
.
Alternatively, you can use the startsWith
function.
tf = 5×1 logical array
0 1 1 1 0
Input Arguments
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
ands2
are string arrays or cell arrays of character vectors, thens1
ands2
must be the same size, unless one of them is scalar. - If both
s1
ands2
are character arrays with multiple rows, thens1
ands2
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
Maximum number of characters to compare, specified as an integer.
- If
n
is0
, thenstrncmp
always returns1
. By convention, the zeroth character of a character vector or a string scalar is always''
, a0
-by-0
character array. - If
n
is less than0
, thenstrncmp
treats it as0
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
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 ann
-by-1
array, wheren
is the number of rows in the character array. - If both inputs are character arrays, then
tf
is a scalar.
Tips
- The
strncmp
function is intended for comparison of text. If used on numeric arrays,strncmp
always returns0
. - For case-insensitive text comparison, use
strncmpi
instead ofstrncmp
. - Although
strncmp
shares a name with a C function, it does not follow the C language convention of returning0
when the text inputs match.
Extended Capabilities
Thestrncmp
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- Enumeration inputs are not supported.
- When one input is a cell array and the other input is a character array, the character array must be a compile-time row vector.
Version History
Introduced before R2006a