Replace: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Bale
Created page
imported>Bale
mNo edit summary
 
(2 intermediate revisions by one other user not shown)
Line 5: Line 5:
FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=String Handling Routines|


function1={{Function|
function1={{Function|
Line 18: Line 17:
parameter3={{Param|int|index2}}|
parameter3={{Param|int|index2}}|
p3desc={{pspan|index2}} is the index of the second character to replace|
p3desc={{pspan|index2}} is the index of the second character to replace|
parameter4={{Param|string|new}} is the new string to insert into that position|
parameter4={{Param|string|new}}|
p4desc={{pspan|new}}|
p4desc={{pspan|new}} is the new string to insert into that position|
}}|
}}|


function_description=Replaces every character in the {{pspan|original}} text between {{Param|int|index1}} and {{Param|int|index2}} with the {{Param|string|new}} string and returns the result.|
function_description=Replaces every character in the {{pspan|original}} text between {{pspan|index1}} and {{pspan|index2}} with the {{pspan|new}} string and returns the result.|


code1={{CodeSample|
code1={{CodeSample|
Line 48: Line 47:
see_also={{SeeAlso|replace_string|index_of|last_index_of}}|
see_also={{SeeAlso|replace_string|index_of|last_index_of}}|
}}
}}
[[Category:String Handling Routines]]

Latest revision as of 05:11, 22 May 2010

Function Syntax

buffer replace(buffer original ,int index1 ,int index2 ,string new )

  • original is the starting string or buffer
  • index1 is the index of the first character to replace
  • index2 is the index of the second character to replace
  • new is the new string to insert into that position

Replaces every character in the original text between index1 and index2 with the new string and returns the result.

Code Sample

Replaces the center image of the Cyrpt (it's blank, don't worry) with some information regarding the noncombats of the zones.

void main() {
   buffer original;
   append(original, visit_url());
 
   string old = "<img src=\"http://images.kingdomofloathing.com/otherimages/cyrpt/cyrpt5.gif\">";
   string cyrpt = "<font size=1>&lt;- Muscle &nbsp; &nbsp; Mys -&gt;<br />"
   + "&lt;- Mox &nbsp; &nbsp; All -&gt;</font>";
   
   int index1 = index_of(original, old);
   
   replace(original, index1, length(old)+ index1, cyrpt);
 
   write(original);
}

See Also

replace_string() | index_of() | last_index_of()