Difference between revisions of "Manual:DIL Manual/left()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = left() = | |
| + | '''Function''' (string). Returns the first characters of a string. | ||
| − | + | left(s : string, n : integer) | |
| − | + | Returns the first <code>n</code> characters of <code>s</code>: <code>left("shorten me", 5)</code> | |
| − | + | is <code>"short"</code>. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | s || string || The string. | ||
| + | |- | ||
| + | | n || integer || How many characters to take. | ||
| + | |} | ||
| − | + | == Returns == | |
| − | + | String: the first <code>n</code> characters. The whole string when <code>n</code> is its length | |
| − | + | or more; <code>""</code> when <code>n</code> is 0 or less, or <code>s</code> is empty. | |
| − | |||
| − | + | == Notes == | |
| − | + | * It counts bytes, so a letter outside ASCII (UTF-8) counts as two or more, and can be cut | |
| − | + | in half. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | dilbegin string shorten(s : string, n : integer); | |
| − | + | code | |
| − | + | { | |
| − | + | if (length(s) <= n) | |
| − | + | return(s); | |
| − | + | return(left(s, n - 3) + "..."); | |
| − | + | } | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
dilend | dilend | ||
| + | </pre> | ||
| + | Shortens a long text to <code>n</code> characters, ending in ''...''. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/right()|right()]] | |
| − | + | * [[Manual:DIL Manual/mid()|mid()]] | |
| − | + | * [[Manual:DIL Manual/length()|length()]] | |
| − | + | * [[Manual:DIL Manual/strncmp()|strncmp()]] | |
Latest revision as of 23:04, 14 November 2026
left()
Function (string). Returns the first characters of a string.
left(s : string, n : integer)
Returns the first n characters of s: left("shorten me", 5)
is "short".
Parameters
| Name | Type | Meaning |
|---|---|---|
| s | string | The string. |
| n | integer | How many characters to take. |
Returns
String: the first n characters. The whole string when n is its length
or more; "" when n is 0 or less, or s is empty.
Notes
- It counts bytes, so a letter outside ASCII (UTF-8) counts as two or more, and can be cut
in half.
Example
dilbegin string shorten(s : string, n : integer);
code
{
if (length(s) <= n)
return(s);
return(left(s, n - 3) + "...");
}
dilend
Shortens a long text to n characters, ending in ....