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

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="bfmid"></span>
+
= mid() =
 +
'''Function''' (string). Returns the characters from one position to another.
  
−
  '''Function:'''  <i><!--CODE-->string mid ( o : string, s : integer, e : integer );</i><!--ENDCODE-->
+
  mid(s : string, first : integer, last : integer)
  
−
<!--TERM--> '''o'''
+
Returns the characters of <code>s</code> from position <code>first</code> to position
−
<!--DEFINITION-->         the original string to be parsed
+
<code>last</code>, both included, counting from 1: <code>mid("sprocket", 3, 6)</code> is
−
<!--TERM--> '''s'''
+
<code>"rock"</code>.
−
<!--DEFINITION-->         The starting point of the string to be parsed out
 
−
<!--TERM--> '''e'''
 
−
<!--DEFINITION-->         the ending point of the string to be parsed out
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->         the portion of the string defined by the 's' and 'e' values
 
  
−
This function parses the string passed to it and returns the portion of the string
+
== Parameters ==
−
  defined by the start value and the end value that is also passed to the function.
+
{| class="wikitable"
−
'''Example:'''  <i><!--CODE-->"rock" := mid ("sprocket",3,6);</i><!--ENDCODE-->
+
! Name !! Type !! Meaning
 +
|-
 +
| s || string || The string.
 +
|-
 +
| first || integer || Position of the first character to take; 1 is the start.
 +
|-
 +
| last || integer || Position of the last character to take.
 +
|}
  
−
---~---~---~---~---~---~---~---~---
+
== Returns ==
−
<span id="bfmid"></span>
+
String: the characters from <code>first</code> to <code>last</code>. A <code>first</code> below 1
 +
counts as 1 and a <code>last</code> past the end as the end; when <code>first</code> is past the
 +
end the result is <code>""</code>. When <code>last</code> is smaller than <code>first</code> it
 +
fails and writes ''Illegal: mid(1,2,3) 3 &gt; 2'' to the zone's log; a string variable assigned
 +
the result is then empty.
  
−
'''Function:'''  <i><!--CODE-->string mid ( o : string, s : integer, e : integer );</i><!--ENDCODE-->
+
== Notes ==
 +
* The third argument is a position, not a length: <code>mid(s, 5, 2)</code> is an error (or
 +
  <code>""</code> when <code>s</code> is shorter than 5).
 +
* It counts bytes, so a letter outside ASCII (UTF-8) counts as two or more.
  
−
<!--TERM--> '''o'''
+
== Example ==
−
<!--DEFINITION-->        the original string to be parsed
+
<pre>
−
<!--TERM-->  '''s'''
+
dilbegin string stamp(line : string);
−
<!--DEFINITION-->        The starting point of the string to be parsed out
+
code
−
<!--TERM-->  '''e'''
+
{
−
<!--DEFINITION-->        the ending point of the string to be parsed out
+
  // asctime(realtime) is like "Sat Nov 14 12:34:56 2026": characters 12 to 16 are "12:34"
−
<!--TERM--> '''return'''
+
  return(mid(asctime(realtime), 12, 16) + " " + line);
−
<!--DEFINITION-->        the portion of the string defined by the 's' and 'e' values
+
}
 +
dilend
 +
</pre>
 +
Puts the time of day in front of a line, as the logs in commands.zon do.
  
−
This function parses the string passed to it and returns the portion of the string
+
== See also ==
−
  defined by the start value and the end value that is also passed to the function.
+
* [[Manual:DIL Manual/left()|left()]]
−
'''Example:'''  <i><!--CODE-->"rock" := mid ("sprocket",3,6);</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/right()|right()]]
−
 
+
* [[Manual:DIL Manual/asctime()|asctime()]]
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/length()|length()]]

Latest revision as of 23:04, 14 November 2026

mid()

Function (string). Returns the characters from one position to another.

mid(s : string, first : integer, last : integer)

Returns the characters of s from position first to position last, both included, counting from 1: mid("sprocket", 3, 6) is "rock".

Parameters

Name Type Meaning
s string The string.
first integer Position of the first character to take; 1 is the start.
last integer Position of the last character to take.

Returns

String: the characters from first to last. A first below 1 counts as 1 and a last past the end as the end; when first is past the end the result is "". When last is smaller than first it fails and writes Illegal: mid(1,2,3) 3 > 2 to the zone's log; a string variable assigned the result is then empty.

Notes

  • The third argument is a position, not a length: mid(s, 5, 2) is an error (or
 "" when s is shorter than 5).
  • It counts bytes, so a letter outside ASCII (UTF-8) counts as two or more.

Example

dilbegin string stamp(line : string);
code
{
   // asctime(realtime) is like "Sat Nov 14 12:34:56 2026": characters 12 to 16 are "12:34"
   return(mid(asctime(realtime), 12, 16) + " " + line);
}
dilend

Puts the time of day in front of a line, as the logs in commands.zon do.

See also