Difference between revisions of "Manual:DIL Manual/replace()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = 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 <code>s</code> in which every <code>old</code> is replaced by | |
| − | + | <code>new</code>. Note the order: what to look for, what to put instead, and last the string to | |
| − | + | work on. <code>s</code> itself is not changed; assign the result. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! Name !! Type !! Meaning | |
| − | + | |- | |
| − | + | | old || string || The text to look for. | |
| − | + | |- | |
| − | + | | new || string || What to put in its place; <code>""</code> deletes <code>old</code>. | |
| + | |- | ||
| + | | s || string || The string to work on. | ||
| + | |} | ||
| − | + | == Returns == | |
| + | String: <code>s</code> with the replacements made. When <code>old</code> is empty, or not in | ||
| + | <code>s</code>, it is <code>s</code> unchanged. | ||
| − | + | == Notes == | |
| − | + | * The search is case-sensitive, unlike <code>==</code> and <code>in</code> on strings. | |
| − | + | * It works from left to right and doesn't search the text it has put in, so | |
| − | + | <code>replace("a", "aa", s)</code> doubles every ''a'' once and stops. | |
| − | + | * A string variable that was never set counts as <code>""</code>. | |
| − | |||
| − | |||
| − | |||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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 | ||
| + | </pre> | ||
| + | Fills in a text with a player's name and level and turns <code>&&</code> into line breaks, | ||
| + | as <code>replace("&&", "<br/>", arg)</code> in comm.zon does. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/left()|left()]] | ||
| + | * [[Manual:DIL Manual/mid()|mid()]] | ||
| + | * [[Manual:DIL Manual/split()|split()]] | ||
| + | * [[Manual:DIL Manual/Operators|Operators]] | ||
Latest revision as of 23:04, 14 November 2026
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.