Difference between revisions of "Manual:DIL Manual/strncmp()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = strncmp() = | |
| + | '''Function''' (integer). Compares the first characters of two strings, minding upper and lower case. | ||
| − | + | strncmp(s1 : string, s2 : string, n : integer) | |
| − | + | Like [[Manual:DIL Manual/strcmp()|strcmp()]], but looks at no more than the first | |
| − | + | <code>n</code> characters of each string. Use it to test how a string begins when case matters. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! Name !! Type !! Meaning | |
| − | + | |- | |
| − | + | | s1 || string || The first string. | |
| − | + | |- | |
| − | + | | s2 || string || The second string. | |
| + | |- | ||
| + | | n || integer || How many characters to compare at most. | ||
| + | |} | ||
| + | == Returns == | ||
| + | Integer: 0 when the first <code>n</code> characters are the same (or both strings end sooner, | ||
| + | alike), less than 0 when <code>s1</code> sorts before <code>s2</code>, more than 0 when after. | ||
| + | Test the sign only. | ||
| − | + | A string variable that was never set counts as <code>""</code>. It fails when a string | |
| − | + | argument is a failed value. | |
| − | |||
| − | |||
| − | |||
| − | + | == Notes == | |
| − | + | * <code>n</code> 0 always gives 0. A negative <code>n</code> compares the whole strings. | |
| + | * A shorter string sorts first: <code>strncmp("ab", "abc", 3)</code> is less than 0. | ||
| + | * For a test that ignores case, <code>"word" in s</code> and <code>left()</code> with | ||
| + | <code>==</code> are simpler. | ||
| − | + | == Example == | |
| + | <pre> | ||
| + | dilbegin integer is_god_tag(s : string); | ||
| + | code | ||
| + | { | ||
| + | return (strncmp(s, "GOD:", 4) == 0); | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Tells whether a text begins with the capitals <code>GOD:</code>, so that <code>god:</code> | ||
| + | doesn't count. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/strcmp()|strcmp()]] | |
| − | + | * [[Manual:DIL Manual/left()|left()]] | |
| − | + | * [[Manual:DIL Manual/in|in]] | |
| − | + | * [[Manual:DIL Manual/Operators|Operators]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:20, 14 November 2026
strncmp()
Function (integer). Compares the first characters of two strings, minding upper and lower case.
strncmp(s1 : string, s2 : string, n : integer)
Like strcmp(), but looks at no more than the first
n characters of each string. Use it to test how a string begins when case matters.
Parameters
| Name | Type | Meaning |
|---|---|---|
| s1 | string | The first string. |
| s2 | string | The second string. |
| n | integer | How many characters to compare at most. |
Returns
Integer: 0 when the first n characters are the same (or both strings end sooner,
alike), less than 0 when s1 sorts before s2, more than 0 when after.
Test the sign only.
A string variable that was never set counts as "". It fails when a string
argument is a failed value.
Notes
n0 always gives 0. A negativencompares the whole strings.- A shorter string sorts first:
strncmp("ab", "abc", 3)is less than 0. - For a test that ignores case,
"word" in sandleft()with
== are simpler.
Example
dilbegin integer is_god_tag(s : string);
code
{
return (strncmp(s, "GOD:", 4) == 0);
}
dilend
Tells whether a text begins with the capitals GOD:, so that god:
doesn't count.