Adonix does not provide an instruction for replacing a string part.
The only existent instruction is
ctrans(STR,"AB","C")
that substitutes into the STR string all occurrences of ‘A’ and ‘B’ letters into ‘C’ letter.
Note that it does not replace string “AB”, but all occurrences of ‘A’ letter and all occurrences of ‘B’ letter.
We remedy this miss implementing a REPLACE function and a his wrapper too,
so we can manage with the same code both simple strings both clobs.
#
#File YSAGEDEV
#
#############################################################################
# Author: SageDev.it
# It substitutes all occurrences of OLD with NEW
# STR is a clob
#############################################################################
Funprog REPLACE(STR, OLD, NEW)
Value Clbfile STR()
Value Char OLD()
Value Char NEW()
Local Integer LENOLD
Local Integer LENNEW
#length of string to substitute
LENOLD= len(OLD)
#length of string that will substitute OLD string
LENNEW= len(NEW)
If LENOLD<=0 or LENOLD>len(STR)
#the string to substitute is empty or it is longer than initial string, so we do nothing
End STR
Endif
Local Integer INDEX
Local Integer INDEXSTART
#We search the string to substitute
INDEX=instr(1,STR,OLD)
While INDEX>0
#INDEX>0 this means that we have found an occurrence of OLD
#We substitute OLD occurrence with the NEW string
STR =left$(STR, INDEX-1) + NEW + right$(STR, INDEX+LENOLD)
#We calc the index to which the NEW string just inserted ends
INDEXSTART=INDEX+LENNEW
#We search the string to substitute starting from the end of last found occurrence
INDEX=instr(INDEXSTART,STR,OLD)
Wend
End STR
#############################################################################
#############################################################################
# Author: SageDev.it
# Wrapper of REPLACE(STR, OLD, NEW)
# It substitutes all OLD occurrence with NEW
# the STR parameter is a CHAR(), not a clob
#############################################################################
Funprog REPLACESTR(STR, OLD, NEW)
Value Char STR()
Value Char OLD()
Value Char NEW()
Local Char RET(250)
Local Clbfile STRCLB(1)
STRCLB = STR
RET = func YSAGEDEV.REPLACE(STRCLB, OLD, NEW)
End RET
#############################################################################