Difference between revisions of "Manual:DIL Manual/replace()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfreplace"></span>
+
= replace() =
 +
'''Function''' (string). Replaces every occurrence of one text in a string with another.
  
−
  '''Function:'''  <i><!--CODE-->string replace( t :string, n : string, o : string);</i><!--ENDCODE-->
+
  replace(old : string, new : string, s : string)
  
−
<!--TERM--> '''t'''
+
Returns a copy of <code>s</code> in which every <code>old</code> is replaced by
−
<!--DEFINITION-->         the target string you want to replace
+
<code>new</code>. Note the order: what to look for, what to put instead, and last the string to
−
<!--TERM--> '''n'''
+
work on. <code>s</code> itself is not changed; assign the result.
−
<!--DEFINITION-->         what you want to replace the target with
 
−
<!--TERM--> '''o'''
 
−
<!--DEFINITION-->         the original string
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->         the string with the old string replaced by the new string
 
  
−
This function replaces all occurences of a string in another string with a
+
== Parameters ==
−
new string.
+
{| class="wikitable"
−
'''Example:'''
+
! Name !! Type !! Meaning
−
<i><!--CODE-->"Jafar %t% %l%" := replace(%n%,pc.name,"%n% %t% %l%");</i><!--ENDCODE-->
+
|-
−
<i><!--CODE-->"Jafar the human %l%" := replace(%t%,pc.title,"Jafar %t% %l%");</i><!--ENDCODE-->
+
| old || string || The text to look for.
−
<i><!--CODE-->"Jafar the human 1" := replace(%l%,itoa(pc.vlevel),"Jafar the human %l%");</i><!--ENDCODE-->
+
|-
−
<span id="bfreplace"></span>
+
| new || string || What to put in its place; <code>""</code> deletes <code>old</code>.
 +
|-
 +
| s || string || The string to work on.
 +
|}
  
−
'''Function:'''  <i><!--CODE-->string replace( t :string, n : string, o : string);</i><!--ENDCODE-->
+
== 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.
  
−
<!--TERM--> '''t'''
+
== Notes ==
−
<!--DEFINITION-->         the target string you want to replace
+
* The search is case-sensitive, unlike <code>==</code> and <code>in</code> on strings.
−
<!--TERM--> '''n'''
+
* It works from left to right and doesn't search the text it has put in, so
−
<!--DEFINITION-->         what you want to replace the target with
+
  <code>replace("a", "aa", s)</code> doubles every ''a'' once and stops.
−
<!--TERM--> '''o'''
+
* A string variable that was never set counts as <code>""</code>.
−
<!--DEFINITION-->        the original string
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->         the string with the old string replaced by the new string
 
  
−
This function replaces all occurences of a string in another string with a
+
== Example ==
−
new string.
+
<pre>
−
'''Example:'''
+
dilbegin string fill_in(text : string, pc : unitptr);
−
<i><!--CODE-->"Jafar %t% %l%" := replace(%n%,pc.name,"%n% %t% %l%");</i><!--ENDCODE-->
+
var
−
<i><!--CODE-->"Jafar the human %l%" := replace(%t%,pc.title,"Jafar %t% %l%");</i><!--ENDCODE-->
+
  s : string;
−
<i><!--CODE-->"Jafar the human 1" := replace(%l%,itoa(pc.vlevel),"Jafar the human %l%");</i><!--ENDCODE-->
+
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>&amp;&amp;</code> into line breaks,
 +
as <code>replace("&amp;&amp;", "&lt;br/&gt;", 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 == 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