Difference between revisions of "Manual:DIL Manual/substring()"
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: | ||
| + | = substring() = | ||
| + | '''Procedure.''' Removes a string from a stringlist. | ||
| + | substring(sl : stringlist, s : string) | ||
| − | < | + | Removes the first element of <code>sl</code> that matches <code>s</code>, ignoring case. Only one element goes per call. <code>sl</code> is a stringlist variable or a stringlist field, such as a unit's <code>names</code>. Despite its name, it has nothing to do with parts of a string; for those see <code>left()</code>, <code>mid()</code> and <code>right()</code>. |
| − | + | ||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! Name !! Type !! Meaning | |
| + | |- | ||
| + | | sl || stringlist || The list: a variable or a field. | ||
| + | |- | ||
| + | | s || string || The string to remove. | ||
| + | |} | ||
| + | |||
| + | == Notes == | ||
| + | * An element matches when it is <code>s</code>, or the first word or words of <code>s</code>: with <code>{"ship", "boat"}</code>, <code>substring(sl, "ship arrived")</code> removes <code>"ship"</code>. An element longer than <code>s</code> doesn't match: <code>"ship"</code> doesn't remove <code>"ship arrived"</code>. | ||
| + | * When nothing matches, or <code>s</code> fails, the list stays as it is. | ||
| + | * To remove every copy, loop while <code>s in sl</code>. | ||
| + | * <code>remove()</code> removes an element by its place in the list. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin greeter(); | ||
| + | var | ||
| + | seen : stringlist; | ||
| + | code | ||
| + | { | ||
| + | :start: | ||
| + | wait(SFB_CMD, (activator.type == UNIT_ST_PC) and (command("say") or command("quit"))); | ||
| + | if (command("quit")) | ||
| + | substring(seen, activator.name); | ||
| + | else if (not (activator.name in seen)) | ||
| + | { | ||
| + | addstring(seen, activator.name); | ||
| + | exec("say Welcome, " + activator.name + "!", self); | ||
| + | } | ||
| + | goto start; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | An NPC that greets each player once, and forgets them when they quit. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/addstring()|addstring()]] | ||
| + | * [[Manual:DIL Manual/remove()|remove()]] | ||
| + | * [[Manual:DIL Manual/in|in]] | ||
| + | * [[Manual:DIL Manual/stringlist|stringlist]] | ||
| + | * [[Manual:DIL Manual/names|names]] | ||
Latest revision as of 23:23, 14 November 2026
substring()
Procedure. Removes a string from a stringlist.
substring(sl : stringlist, s : string)
Removes the first element of sl that matches s, ignoring case. Only one element goes per call. sl is a stringlist variable or a stringlist field, such as a unit's names. Despite its name, it has nothing to do with parts of a string; for those see left(), mid() and right().
Parameters
| Name | Type | Meaning |
|---|---|---|
| sl | stringlist | The list: a variable or a field. |
| s | string | The string to remove. |
Notes
- An element matches when it is
s, or the first word or words ofs: with{"ship", "boat"},substring(sl, "ship arrived")removes"ship". An element longer thansdoesn't match:"ship"doesn't remove"ship arrived". - When nothing matches, or
sfails, the list stays as it is. - To remove every copy, loop while
s in sl. remove()removes an element by its place in the list.
Example
dilbegin greeter();
var
seen : stringlist;
code
{
:start:
wait(SFB_CMD, (activator.type == UNIT_ST_PC) and (command("say") or command("quit")));
if (command("quit"))
substring(seen, activator.name);
else if (not (activator.name in seen))
{
addstring(seen, activator.name);
exec("say Welcome, " + activator.name + "!", self);
}
goto start;
}
dilend
An NPC that greets each player once, and forgets them when they quit.