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

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
 
(checked against the compiler and the interpreter; example compiles)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 +
= string in string =
 +
'''Operator''' (integer).
  
 +
s1 in s2 : integer
  
−
<span id="esins"></span>
+
TRUE (1) when the text <code>s1</code> appears anywhere in <code>s2</code>, FALSE (0) when it doesn't. Case is ignored, and <code>s1</code> may be part of a word: <code>"key" in "Do you have a KEY?"</code> and <code>"ear" in "I hear you"</code> are both TRUE.
−
string 'in' string
 
−
    Argument 1: A string to find.
 
−
    Argument 2: A string to search.
 
−
    Return: TRUE, if first string is found in second string.
 
  
−
'''Example:'''
+
== Returns ==
 +
TRUE or FALSE. An empty <code>s1</code> (<code>""</code>) is found in every string.
  
−
<nowiki>
+
== Notes ==
 +
* It is a plain substring test: to match whole words, use <code>in</code> with a stringlist (for example one made with <code>split()</code>) instead.
 +
* A string variable that was never assigned has no value at all, which isn't the same as <code>""</code>. With such a variable on either side the result is null instead of an integer: an <code>if</code> treats it as false, but assigning it to an integer variable stops the program with "Type error in integer assignemt" in the log.
 +
* The same operator with a stringlist or an extra description list on the right works differently: see the pages below.
  
−
  dilbegin foo();
+
== Example ==
−
  code
+
<pre>
−
  {
+
dilbegin keyword();
−
    if ("guard" in activator.title)
+
code
−
      exec("say hello guard",self);
+
{
−
    pause;
+
  heartbeat := PULSE_SEC * 5;
−
  }
 
−
  dilend</nowiki>
 
  
−
---~---~---~---~---~---~---~---~---
+
  :start:
 +
  wait(SFB_CMD, command("say") and (activator.type == UNIT_ST_PC));
 +
 
 +
  if ("password" in argument)
 +
      exec("say Shh! Not so loud!", self);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
A monster that reacts when a player says something with "password" in it.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/string in stringlist|string in stringlist]]
 +
* [[Manual:DIL Manual/string in extraptr|string in extraptr]]
 +
* [[Manual:DIL Manual/in|in]]
 +
* [[Manual:DIL Manual/Operators|Operators]]
 +
* [[Manual:DIL Manual/strcmp()|strcmp()]]

Latest revision as of 23:24, 14 November 2026

string in string

Operator (integer).

s1 in s2 : integer

TRUE (1) when the text s1 appears anywhere in s2, FALSE (0) when it doesn't. Case is ignored, and s1 may be part of a word: "key" in "Do you have a KEY?" and "ear" in "I hear you" are both TRUE.

Returns

TRUE or FALSE. An empty s1 ("") is found in every string.

Notes

  • It is a plain substring test: to match whole words, use in with a stringlist (for example one made with split()) instead.
  • A string variable that was never assigned has no value at all, which isn't the same as "". With such a variable on either side the result is null instead of an integer: an if treats it as false, but assigning it to an integer variable stops the program with "Type error in integer assignemt" in the log.
  • The same operator with a stringlist or an extra description list on the right works differently: see the pages below.

Example

dilbegin keyword();
code
{
   heartbeat := PULSE_SEC * 5;

   :start:
   wait(SFB_CMD, command("say") and (activator.type == UNIT_ST_PC));

   if ("password" in argument)
      exec("say Shh! Not so loud!", self);
   goto start;
}
dilend

A monster that reacts when a player says something with "password" in it.

See also