strncmp — Compare up to N leading characters of text, case-sensitively.
strncmp(A, B, N) returns logical prefix-comparison results with compatible text-container expansion.
Syntax
tf = strncmp(A, B, N)Inputs
A- Type
Any- Required
- Yes
- Description
- First text operand.
B- Type
Any- Required
- Yes
- Description
- Second text operand.
N- Type
IntegerScalar- Required
- Yes
- Description
- Maximum number of leading characters to compare.
Returns
tf- Type
LogicalArray- Description
- Logical prefix-comparison result.
Errors
RunMat:strncmp:InvalidInput- When
- At least one text input is not a supported text container.
- Message
strncmp: text inputs must be string/char/cell/string-array values
RunMat:strncmp:ShapeMismatch- When
- Text inputs are not broadcast-compatible.
- Message
strncmp: input sizes are not broadcast-compatible
RunMat:strncmp:InvalidPrefixLength- When
- Prefix length is not a finite integral scalar representable as a host count.
- Message
strncmp: prefix length must be a finite nonnegative integer scalar
Behavior
strncmp(A, B, N) compares at most N leading Unicode scalar values from each corresponding text element, case-sensitively. It accepts string scalars and arrays, character vectors and matrices, and cell arrays of character vectors. Scalar text expands against compatible arrays; character-matrix rows are independent text elements.
N accepts real scalar double, single, logical, and all eight fixed-width integer classes. Native integer storage is read exactly. A negative integral N is treated as zero; zero makes every element comparison true, including missing strings. Unrepresentable host counts and nonintegral or nonfinite floating values reject; a floating count is not rounded to a nearby integer.
For positive N, missing string elements compare false. If both text elements end before N and all compared characters matched, the result is true; an extra character on either side before N makes it false.
Unsupported direct numeric or provider-resident operands in A or B return scalar logical false before N parsing or provider access. N may be a supported resident scalar and is gathered before exact validation; no string comparison runs on a GPU.
Placement and effects
This default binding performs host text comparison in native and browser runtimes. Gathering a supported resident N may suspend before host validation; there is no GPU text kernel or fusion form. It has no callback, workspace, filesystem, randomness, network, or time effects. Invalid N or incompatible text shape can throw.
Zero requested outputs evaluates and discards the result; more than one output is invalid. Ordinary worker transfer of host text is separate from unsupported distributed-input execution.
Examples
Compare leading characters
tf = strncmp("RunMat", "Runway", 3)Expected output:
tf = logical 1Expand a prefix across strings
names = ["north" "south" "east"]; tf = strncmp(names, "no", 2)Expected output:
tf = [true false false]Compare rows of a character matrix
animals = char("cat", "camel", "cow"); tf = strncmp(animals, "ca", 2)Expected output:
tf = [true; true; false]Compare cell arrays
C1 = {'red', 'green', 'blue'}; C2 = {'rose', 'grey', 'black'}; tf = strncmp(C1, C2, 2)Expected output:
tf = [false true true]A zero-length comparison succeeds
tf = strncmp("alpha", "omega", 0)Expected output:
tf = logical 1Use an exact integer prefix count
tf = strncmp("RunMat", "Runway", uint64(3))Expected output:
tf = logical 1Using strncmp with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strncmp changes the result.
Run a small strncmp example, explain the result, then change one input and compare the output.
FAQ
What types can I pass to strncmp?⌄
A and B accept string arrays, character vectors or matrices, and cell arrays of character vectors. N accepts real integral scalar double, single, or fixed-width integer; negative values act as zero.
Is comparison case-sensitive?⌄
Yes. Use strncmpi to ignore letter case.
What if N is zero?⌄
Every element comparison is true because no characters are compared.
What if a string ends before N?⌄
It compares equal only if the other string also ends at that point after the same characters.
How are missing strings treated?⌄
For positive N, any comparison involving a missing string is false. For zero N, every comparison is true.
What is the result class?⌄
Logical; scalar inputs yield a logical scalar and expanded inputs yield a logical array in column-major order.
Related Strings functions
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmpi · strtok · unicode2native
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Source you can read
Every RunMat function's source is public. Read exactly how strncmp is executed, line by line, in Rust.
- View the source for strncmp in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.