Difference between revisions of "Manual:DIL Manual/mid()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = mid() = | |
| + | '''Function''' (string). Returns the characters from one position to another. | ||
| − | + | mid(s : string, first : integer, last : integer) | |
| − | + | Returns the characters of <code>s</code> from position <code>first</code> to position | |
| − | + | <code>last</code>, both included, counting from 1: <code>mid("sprocket", 3, 6)</code> is | |
| − | + | <code>"rock"</code>. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! 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 <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 > 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: <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. | ||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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 | ||
| + | </pre> | ||
| + | Puts the time of day in front of a line, as the logs in commands.zon do. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/left()|left()]] | |
| − | + | * [[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
""whensis 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.