Manual:DIL Manual/replace()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:04, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

replace()

Function (string). Replaces every occurrence of one text in a string with another.

replace(old : string, new : string, s : string)

Returns a copy of s in which every old is replaced by new. Note the order: what to look for, what to put instead, and last the string to work on. s itself is not changed; assign the result.

Parameters

Name Type Meaning
old string The text to look for.
new string What to put in its place; "" deletes old.
s string The string to work on.

Returns

String: s with the replacements made. When old is empty, or not in s, it is s unchanged.

Notes

  • The search is case-sensitive, unlike == and in on strings.
  • It works from left to right and doesn't search the text it has put in, so
 replace("a", "aa", s) doubles every a once and stops.
  • A string variable that was never set counts as "".

Example

dilbegin string fill_in(text : string, pc : unitptr);
var
   s : string;
code
{
   s := replace("%n", pc.name, text);
   s := replace("%l", itoa(pc.vlevel), s);
   s := replace("&&", "<br/>", s);
   return(s);
}
dilend

Fills in a text with a player's name and level and turns && into line breaks, as replace("&&", "<br/>", arg) in comm.zon does.

See also