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

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="bfleft"></span>
+
= left() =
 +
'''Function''' (string). Returns the first characters of a string.
  
−
  '''Function:'''  <i><!--CODE-->string left ( o : string, l : integer );</i><!--ENDCODE-->
+
  left(s : string, n : integer)
  
−
<!--TERM--> '''o'''
+
Returns the first <code>n</code> characters of <code>s</code>: <code>left("shorten me", 5)</code>
−
<!--DEFINITION-->         the original string to be parsed
+
is <code>"short"</code>.
−
<!--TERM--> '''l'''
 
−
<!--DEFINITION-->         The amount of characters to parse out
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->         the left portion of the string with length l
 
  
−
This function parses the string passed to it and returns the number
+
== Parameters ==
−
of characters defined in its second argument.
+
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| s || string || The string.
 +
|-
 +
| n || integer || How many characters to take.
 +
|}
  
−
'''Example:'''  <i><!--CODE-->"short" := left ("shorten me",5);</i><!--ENDCODE-->
+
== Returns ==
−
'''Example:'''
+
String: the first <code>n</code> characters. The whole string when <code>n</code> is its length
−
<i><!--CODE-->
+
or more; <code>""</code> when <code>n</code> is 0 or less, or <code>s</code> is empty.
−
---~---~---~---~---~---~---~---~---
 
  
−
dilbegin aware describe (arg:string);
+
== Notes ==
−
var
+
* It counts bytes, so a letter outside ASCII (UTF-8) counts as two or more, and can be cut
−
        side:string;
+
  in half.
−
        oneword:stringlist;
 
−
        location:string;
 
−
        ln:integer;
 
−
        args:stringlist;
 
−
        temp:string;
 
−
        i:integer;
 
−
        x:extraptr;
 
−
code
 
−
dilend
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="bfleft"></span>
 
−
 
 
−
'''Function:'''  <i><!--CODE-->string left ( o : string, l : integer );</i><!--ENDCODE-->
 
−
 
 
−
<!--TERM-->  '''o'''
 
−
<!--DEFINITION-->        the original string to be parsed
 
−
<!--TERM-->  '''l'''
 
−
<!--DEFINITION-->        The amount of characters to parse out
 
−
<!--TERM-->  '''return'''
 
−
<!--DEFINITION-->        the left portion of the string with length l
 
  
−
This function parses the string passed to it and returns the number
+
== Example ==
−
of characters defined in its second argument.
+
<pre>
−
 
+
dilbegin string shorten(s : string, n : integer);
−
'''Example:'''  <i><!--CODE-->"short" := left ("shorten me",5);</i><!--ENDCODE-->
+
code
−
'''Example:'''
+
{
−
<i><!--CODE-->
+
  if (length(s) <= n)
−
---~---~---~---~---~---~---~---~---
+
      return(s);
−
 
+
  return(left(s, n - 3) + "...");
−
dilbegin aware describe (arg:string);
+
}
−
var
 
−
        side:string;
 
−
        oneword:stringlist;
 
−
        location:string;
 
−
        ln:integer;
 
−
        args:stringlist;
 
−
        temp:string;
 
−
        i:integer;
 
−
        x:extraptr;
 
−
code
 
 
dilend
 
dilend
 +
</pre>
 +
Shortens a long text to <code>n</code> characters, ending in ''...''.
  
−
---~---~---~---~---~---~---~---~---
+
== See also ==
−
 
+
* [[Manual:DIL Manual/right()|right()]]
−
</i><!--ENDCODE-->
+
* [[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 ....

See also