Replace

From CometWiki

(Difference between revisions)
Jump to: navigation, search
Jim (Talk | contribs)
(Initial Construction of page)
Newer edit →

Revision as of 16:25, 16 May 2011

Replace

The Replace function is used to replace names in a string with corresponding values, using a MAP to designate name/value pairs.

Length dynamic & local section$ output$

Top: Map _					
 '**Title**',    Title$;_
 '**Name**',     Name$;_
 '**Date**',     Date$;_
 '**Prog**',     Prog$;_
 '**Partition**',Partition$;_
 '**Email**',    Eml$;_
 '**Time**',     time$;_
 '**timeout**',  timeout

cust: Map _
 '**cusnum**,    cusnum$;_
 '**custname**,  custname$
  
output$ = replace(top,section$)
output$ = replace(cust,output$)

Print output$

Will replace all of those names found in the map named "top" with their corresponding values, and then replace the customer info as well before sending the text to the browser. In the REPLACE function, names are case insensitive. If a given name is not found, nothing will change. The above example uses asterisks ("**") to make the names unique and to imitate the legacy xap merge control, but any string can be used as the name.

The replace function is general purpose and can be used in any program to manipulate string data. As a bonus, here is a small Comet32 proc that will replace multiple instances of one substring with another in a target string.


   ! Replaces All Instances Of From$ With To$ In The Target String.
String Replacex(From$, To$, Target$)
   Map: Map From$,To$
   Procreturn Replace(Map,Target$)
End

Once this Proc is included in your program, all you have to do to call it is this:

a$ = replacex('his','my',b$)
Personal tools