Difference between revisions of "Manual:DIL Manual/strncmp()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfstrncmp"></span>
+
= strncmp() =
 +
'''Function''' (integer). Compares the first characters of two strings, minding upper and lower case.
  
−
  '''Function:'''  <i><!--CODE-->integer strcmp( s1:string, s2:string l :integer) ;</i><!--ENDCODE-->
+
  strncmp(s1 : string, s2 : string, n : integer)
  
−
<!--TERM-->  '''s1'''
+
Like [[Manual:DIL Manual/strcmp()|strcmp()]], but looks at no more than the first
−
<!--DEFINITION-->        first string
+
<code>n</code> characters of each string. Use it to test how a string begins when case matters.
−
<!--TERM--> '''s2'''
 
−
<!--DEFINITION-->         second string
 
−
<!--TERM-->  '''l'''
 
−
<!--DEFINITION-->        amount of significant digits to compare
 
  
−
  '''returns'''
+
== Parameters ==
−
<!--TERM-->  '''-1'''
+
{| class="wikitable"
−
<!--DEFINITION-->        if s1 &lt; s2
+
! Name !! Type !! Meaning
−
<!--TERM-->  '''0'''
+
|-
−
<!--DEFINITION-->        if s1 = s2
+
| s1 || string || The first string.
−
<!--TERM-->  '''1'''
+
|-
−
<!--DEFINITION-->        if s1 &gt; s2
+
| 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.
  
−
This allows you to compare strings with case sensitivity in place and it
+
A string variable that was never set counts as <code>""</code>. It fails when a string
−
allows you to choose how much of the strings are compared.
+
argument is a failed value.
−
'''example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
  
−
if (strcmp("Mark Carper",s2,4)==0))
+
== 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.
  
−
</i><!--ENDCODE-->
+
== 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()]]
−
<span id="bfstrncmp"></span>
+
* [[Manual:DIL Manual/in|in]]
−
 
+
* [[Manual:DIL Manual/Operators|Operators]]
−
'''Function:'''  <i><!--CODE-->integer strcmp( s1:string, s2:string l :integer) ;</i><!--ENDCODE-->
 
−
 
 
−
<!--TERM-->  '''s1'''
 
−
<!--DEFINITION-->        first string
 
−
<!--TERM-->  '''s2'''
 
−
<!--DEFINITION-->        second string
 
−
<!--TERM-->  '''l'''
 
−
<!--DEFINITION-->        amount of significant digits to compare
 
−
 
 
−
  '''returns'''
 
−
<!--TERM-->  '''-1'''
 
−
<!--DEFINITION-->        if s1 &lt; s2
 
−
<!--TERM-->  '''0'''
 
−
<!--DEFINITION-->        if s1 = s2
 
−
<!--TERM-->  '''1'''
 
−
<!--DEFINITION-->        if s1 &gt; s2
 
−
 
 
−
 
 
−
This allows you to compare strings with case sensitivity in place and it
 
−
allows you to choose how much of the strings are compared.
 
−
'''example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
if (strcmp("Mark Carper",s2,4)==0))
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
 
 
−
 
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

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

  • n 0 always gives 0. A negative n compares the whole strings.
  • A shorter string sorts first: strncmp("ab", "abc", 3) is less than 0.
  • For a test that ignores case, "word" in s and left() 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.

See also