The following proc will replace all occurrences of 'X' with 'Y' in a string. Note that 'X' and 'Y' do not have to have the same lengths, and that 'Y' could even be null.
The calling sequence is:
call EXAMINE_REPLACE(string, "X", "Y")
Here is the code for the proc itself:-
;-----------------------------------------------------------------------
; examine STRING replacing "X" with "Y"
;-----------------------------------------------------------------------
params
string pio_string_in : INOUT
string pi_old_text : IN
string pi_new_text : IN
endparams
variables
string lv_input_area, lv_hold, lv_output_area
numeric lv_old_text_len
endvariables
lv_input_area = pio_string_in ; move to work area
length pi_old_text
lv_old_text_len = $result ; store length of old text
while (lv_input_area != "") ; repeat until empty
scan lv_input_area, pi_old_text ; look for old_text
if ($result < 1) ; not found
lv_hold = lv_input_area ; use whole string
lv_input_area = "" ; nothing left
else ; found
$result = $result -1 ; backspace over old text
lv_hold = lv_input_area[1:$result] ; extract pre-old text
lv_hold = "%%lv_hold%%pi_new_text" ; append new text
$result = $result + lv_old_text_len ; add length of old text
$result = $result +1 ; skip over old text
lv_input_area = lv_input_area[$result] ; drop pre-old text
endif
lv_output_area = "%%lv_output_area%%lv_hold"
endwhile
pio_string_in = lv_output_area ; replace with adjusted string
return(0)
;-----------------------------------------------------------------------
; Name: EXAMINE_REPLACE
;
; Author: Tony Marston
;
; Date: 20-06-99
;
; Current Version: 1.0.0
;
; Description: Examine STRING replacing all occurrences of
; old_text with new_text
;
; Input Parameters: STRING - string containing old_text
; pi_old_text - to be replaced with new_text
; pi_new_text - replacement for old_text
;
; Output Parameters: STRING
;
; Update History:
; Date Updated By Details
;
Tony Marston
6th June 2000
mailto:tony@tonymarston.net
mailto:TonyMarston@hotmail.com
http://www.tonymarston.net