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

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
 
Line 1: Line 1:
 +
== 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.
  
<span id="str"></span>
+
Static strings are defined just as in the rest of the zone, within double
  '''String:'''
+
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.
  
    A string is some text. They are used for a lot of things such as command
+
=== Example: ===
arguments, room descriptions etc. String variables are automatically resized and
+
   "This is a static 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 may also be added to each other, in an expression.
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:'''
+
=== Example: ===
 +
  "say "+itoa(42)+" is the answer!"
  
  "This is a static string"
+
=== Example: ===
  
Strings may also be added to each other, in an expression.
+
  if (self.name == self.names.[1]) ...
  
'''Example:'''
+
Elements of the string can also be accessed by referencing them by their position.
  
  "say "+itoa(42)+" is the answer!"
+
=== Example ===
 +
  if (str.[3]==f])
 +
  {
 +
    exec ("say The 4th element is a F.",self);
 +
  }
  
  '''Example:'''
+
=== 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.
  
  if (self.name == self.names.[1]) ...
+
=== Example ===
 
+
  i:=0;
  Elements of the string can also be accessed by referencing them by their position.
+
  ln:=length (str);
 
+
  while (str.[i]<ln)
  '''Example'''
+
  {
  if (str.[3]==f])
+
    if (str.[i]=="f")
  {
+
      newstr.[i]:="b";
  exec ("say The 4th element is a F.",self);
+
    else
 +
      newstr:=str.[i];
 +
    i := i + 1;
 
   }
 
   }
 +
  str:=newstr;
  
  '''Note'''
+
This snip of DIL would replace any 'f' in a string with a 'b' remember DIL is
  Currently you can only access the single elements of a string you can not set
+
not case sensitive so it will also replace an 'F'.
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)
 
{
 
if (str.[i]=="f")
 
newstr.[i]:="b";
 
else
 
newstr:=str.[i];
 
i:=i+1;
 
}
 
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 06:53, 29 November 2025

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])
 {
   exec ("say The 4th element is a F.",self);
 }

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)
 {
   if (str.[i]=="f")
     newstr.[i]:="b";
   else
     newstr:=str.[i];
   i := i + 1;
  }
  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'.