Difference between revisions of "Manual:DIL Manual/string"

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="str"></span>
+
= string =
−
'''String:'''
+
'''Type.''' A piece of text.
  
−
    A string is some text. They are used for a lot of things such as command
+
  var
−
  arguments, room descriptions etc. String variables are automatically resized and
+
    s : string;
−
allocated when you assign them, so you do not have to worry about the string
 
−
length nor allocation in your DIL programs. Strings may be compared either with
 
−
'==' (equal to),
 
−
'!=' (not equal),
 
−
'<=' (less than or equal to),
 
−
'>=' (greater than or equal to)
 
−
'<' (less than),
 
−
  '>' (greater than.
 
  
−
  Static strings are defined just as in the rest of the zone, within double
+
Strings are written between double quotes: <code>"Hello there"</code>. A string variable holds a copy of its text: assigning one string variable to another, or passing a string to a template, copies it. An unset string variable is the empty string <code>""</code>.
−
quotations. Strings may be searched easily with the 'in' operator.  Variables
 
−
of type string are saved with DIL programs, if attached to a saved unit.
 
  
−
'''Example:'''
+
== Operators ==
 +
* <code>+</code> joins strings: <code>"You have " + itoa(n) + " coins."</code>. Both sides must be strings; numbers go through <code>itoa()</code>.
 +
* <code>==</code> and <code>!=</code> ignore case: <code>"Sword" == "sword"</code> is true. <code>&lt; &gt; &lt;= &gt;=</code> do not ignore case. <code>strcmp()</code> and <code>strncmp()</code> compare exactly.
 +
* <code>"key" in s</code> tells whether the first string occurs anywhere in the second, ignoring case (1 or 0).
 +
* <code>s.[i]</code> is the character at position <code>i</code> (from 0) as a one-letter string. It can be read but not assigned (<code>s.[1] := "x"</code> is "Illegal lvalue").
  
−
  "This is a static string"
+
== Notes ==
 +
* A <code>"</code> can't be written inside a string: vmc stops at <code>\"</code>. Use single quotes in the text.
 +
* A <code>^</code> inside a string ends the program early (see [[Manual:DIL Manual/dilend|dilend]]).
 +
* Reading <code>s.[i]</code> past the end fails and writes "index of stringlist out of bounds" to the zone log; check <code>length(s)</code> first.
 +
* If any part of a <code>+</code> fails (a field of a null unit, say), the whole result fails, and a <code>sendtext()</code> or assignment with it does nothing.
 +
* Text sent to players is HTML: lines end with <code>&lt;br/&gt;</code>.
 +
* String variables keep their value through waits and are saved with the unit.
  
−
Strings may also be added to each other, in an expression.
+
== Example ==
 +
<pre>
 +
dilbegin do_shout2(arg : string);
 +
var
 +
  s : string;
 +
code
 +
{
 +
  if (arg == "")
 +
  {
 +
      sendtext("Shout what?<br/>", self);
 +
      quit;
 +
  }
 +
  if ("password" in arg)
 +
  {
 +
      sendtext("Better not.<br/>", self);
 +
      quit;
 +
  }
 +
  s := toupper(arg.[0]) + right(arg, length(arg) - 1);
 +
  act("$1n bellows, '$2t!'", A_ALWAYS, self, s, null, TO_ALL);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
A command that refuses some words, gives the text a capital letter and shows it to the room.
  
−
'''Example:'''
+
== See also ==
−
 
+
* [[Manual:DIL Manual/stringlist|stringlist]]
−
  "say "+itoa(42)+" is the answer!"
+
* [[Manual:DIL Manual/length()|length()]]
−
 
+
* [[Manual:DIL Manual/itoa()|itoa()]]
−
'''Example:'''
+
* [[Manual:DIL Manual/atoi()|atoi()]]
−
 
+
* [[Manual:DIL Manual/in|in]]
−
  if (self.name == self.names.[1]) ...
+
* [[Manual:DIL Manual/left()|left()]]
−
 
+
* [[Manual:DIL Manual/right()|right()]]
−
  Elements of the string can also be accessed by referencing them by their position.
+
* [[Manual:DIL Manual/mid()|mid()]]
−
 
+
* [[Manual:DIL Manual/replace()|replace()]]
−
  '''Example'''
+
* [[Manual:DIL Manual/getword()|getword()]]
−
  if (str.[3]==f])
+
* [[Manual:DIL Manual/split()|split()]]
−
'''Note'''
+
* [[Manual:DIL Manual/strcmp()|strcmp()]]
−
  Currently you can only access the single elements of a string you can not set
+
* [[Manual:DIL Manual/Operators|Operators]]
−
them.  In the future this will be possible.  You can still accomplish this by
 
−
copying one string to the other one element at a time and changing any that you
 
−
want something like this.
 
−
 
 
−
'''Example'''
 
−
i:=0;
 
−
ln:=length (str);
 
−
while (str.[i]<ln)
 
−
str:=newstr;
 
−
 
 
−
  This snip of Dil would replace any 'f' in a string with a 'b' remember dil is
 
−
not case sensitive so it will also replace an 'F'.
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="str"></span>
 
−
'''String:'''
 
−
 
 
−
    A string is some text. They are used for a lot of things such as command
 
−
arguments, room descriptions etc. String variables are automatically resized and
 
−
allocated when you assign them, so you do not have to worry about the string
 
−
length nor allocation in your DIL programs. Strings may be compared either with
 
−
'==' (equal to),
 
−
'!=' (not equal),
 
−
'<=' (less than or equal to),
 
−
'>=' (greater than or equal to)
 
−
'<' (less than),
 
−
  '>' (greater than.
 
−
 
 
−
  Static strings are defined just as in the rest of the zone, within double
 
−
quotations.  Strings may be searched easily with the 'in' operator.  Variables
 
−
of type string are saved with DIL programs, if attached to a saved unit.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  "This is a static string"
 
−
 
 
−
Strings may also be added to each other, in an expression.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  "say "+itoa(42)+" is the answer!"
 
−
 
 
−
'''Example:'''
 
−
 
 
−
  if (self.name == self.names.[1]) ...
 
−
 
 
−
  Elements of the string can also be accessed by referencing them by their position.
 
−
 
 
−
  '''Example'''
 
−
  if (str.[3]==f])
 
−
'''Note'''
 
−
  Currently you can only access the single elements of a string you can not set
 
−
them.  In the future this will be possible.  You can still accomplish this by
 
−
copying one string to the other one element at a time and changing any that you
 
−
want something like this.
 
−
 
 
−
'''Example'''
 
−
i:=0;
 
−
ln:=length (str);
 
−
while (str.[i]<ln)
 
−
str:=newstr;
 
−
 
 
−
  This snip of Dil would replace any 'f' in a string with a 'b' remember dil is
 
−
not case sensitive so it will also replace an 'F'.
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Revision as of 23:27, 14 November 2026

string

Type. A piece of text.

var
   s : string;

Strings are written between double quotes: "Hello there". A string variable holds a copy of its text: assigning one string variable to another, or passing a string to a template, copies it. An unset string variable is the empty string "".

Operators

  • + joins strings: "You have " + itoa(n) + " coins.". Both sides must be strings; numbers go through itoa().
  • == and != ignore case: "Sword" == "sword" is true. < > <= >= do not ignore case. strcmp() and strncmp() compare exactly.
  • "key" in s tells whether the first string occurs anywhere in the second, ignoring case (1 or 0).
  • s.[i] is the character at position i (from 0) as a one-letter string. It can be read but not assigned (s.[1] := "x" is "Illegal lvalue").

Notes

  • A " can't be written inside a string: vmc stops at \". Use single quotes in the text.
  • A ^ inside a string ends the program early (see dilend).
  • Reading s.[i] past the end fails and writes "index of stringlist out of bounds" to the zone log; check length(s) first.
  • If any part of a + fails (a field of a null unit, say), the whole result fails, and a sendtext() or assignment with it does nothing.
  • Text sent to players is HTML: lines end with <br/>.
  • String variables keep their value through waits and are saved with the unit.

Example

dilbegin do_shout2(arg : string);
var
   s : string;
code
{
   if (arg == "")
   {
      sendtext("Shout what?<br/>", self);
      quit;
   }
   if ("password" in arg)
   {
      sendtext("Better not.<br/>", self);
      quit;
   }
   s := toupper(arg.[0]) + right(arg, length(arg) - 1);
   act("$1n bellows, '$2t!'", A_ALWAYS, self, s, null, TO_ALL);
   quit;
}
dilend

A command that refuses some words, gives the text a capital letter and shows it to the room.

See also