Difference between revisions of "Replace string"

From Kolmafia
Jump to navigation Jump to search
imported>Heeheehee
m (Formatting and stuff. (Template-breaking is baaad.))
imported>Fredg1
 
(5 intermediate revisions by 4 users not shown)
Line 5: Line 5:
 
FunctionPage|
 
FunctionPage|
 
name={{#var:name}}|
 
name={{#var:name}}|
function_category=String Handling Routines|
 
  
 
function1={{Function|
 
function1={{Function|
Line 30: Line 29:
 
}}|
 
}}|
  
function_description=Searches through the supplied {{pspan|original}} text, replacing every instance of {{pspan|find}} with {{pspan|replace}}, and returns the result.|
+
function_description=Searches through the supplied {{pspan|original}} text, replacing every instance of {{pspan|find}} with {{pspan|replace}}, and returns the result. The result being a buffer, if {{pspan|original}} is a string, you will need to store the result in a variable (buffer) manually, or you will have wasted your time. If it was a buffer, the change(s) will be directly applied (and saved) to {{pspan|original}}.|
 
code1={{CodeSample|
 
code1={{CodeSample|
 
title=Code Sample|
 
title=Code Sample|
description=Will place your Fullness and Spleen above the HP Image|
+
description=Replaces the center image of the Cyrpt (it's blank, don't worry) with some information regarding the noncombats of the zones.|
 
code=<syntaxhighlight>
 
code=<syntaxhighlight>
 
void main()
 
void main()
Line 39: Line 38:
 
   buffer results;
 
   buffer results;
 
   results.append(visit_url());
 
   results.append(visit_url());
   string hp_gif = "<img src=\"/images/itemimages/hp.gif";
+
 
  if ( my_fullness() >= 1 ) string full = "<tr>Fullness: <b>"+my_fullness()+"/"+fullness_limit()+"</b>";
+
   string cyrpt = "<font size=1>&lt;- Muscle &nbsp; &nbsp; Mys -&gt;<br />&lt;- Mox &nbsp; &nbsp; All -&gt;</font>";
   if ( my_spleen_use() >= 1 ) string spleen = "<tr>Spleen: <b>"+my_spleen_use()+"/"+spleen_limit()+"</b>";
+
   results.replace_string("<img src=\"http://images.kingdomofloathing.com/otherimages/cyrpt/cyrpt5.gif\">", cyrpt);
  results.insert(results.index_of(hp_gif), full + "<br />" + spleen + "<br />");
+
 
   results.write();
 
   results.write();
 
}
 
}
Line 49: Line 48:
 
special=Matches are made left-to-right, and once a portion of the supplied {{pspan|original}} is noted as a match, searching continues from the next character after said match.|
 
special=Matches are made left-to-right, and once a portion of the supplied {{pspan|original}} is noted as a match, searching continues from the next character after said match.|
 
}}
 
}}
{{RFI|Is the special note correct, or is the behavior different?|I'm having a hard time clarifying what I mean... see Discussion page}}
+
 
 +
[[Category:String Handling Routines]]

Latest revision as of 06:11, 21 November 2019

Function Syntax

buffer replace_string(buffer original ,string find ,string replace )

buffer replace_string(string original ,string find ,string replace )

  • original is the starting string or buffer
  • find is the text to find in original
  • replace is the text to substitute for find

Searches through the supplied original text, replacing every instance of find with replace, and returns the result. The result being a buffer, if original is a string, you will need to store the result in a variable (buffer) manually, or you will have wasted your time. If it was a buffer, the change(s) will be directly applied (and saved) to original.

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 results;
   results.append(visit_url());

   string cyrpt = "<font size=1>&lt;- Muscle &nbsp; &nbsp; Mys -&gt;<br />&lt;- Mox &nbsp; &nbsp; All -&gt;</font>";
   results.replace_string("<img src=\"http://images.kingdomofloathing.com/otherimages/cyrpt/cyrpt5.gif\">", cyrpt);
	
   results.write();
}

Special

Matches are made left-to-right, and once a portion of the supplied original is noted as a match, searching continues from the next character after said match.