Manual:DIL Manual/replace()
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
==andinon 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.