How to replace entire occurence of a character.

Ram Prashanth

New Member
When I am using replace function it is replacing all the occurrences in the string, I just want the character to be replaced if entire character is found in the string.

ctest = "demo,demo1,system,test"
replace(ctest,"demo","admin").

This gives ctest="admin,admin1,system,test".

I wanted ctest = "admin,demo1,sytem,test".

Is there any way i could achieve this.
 
I dont think you can do that using pre-defined function. Try the below code.

def var ctest as char.
def var a as char.
def var b as char.
def var c as char.
def var i as int.

a = "demo". /*needs to be replaced*/
b = "admin". /*replaced with*/

ctest = "demo1,demo,demo2,test".

do i = 1 to num-entries(ctest):
if entry(i,ctest) = a then do:
if i = 1 then c = b.
else c = c + "," + b.
end.
else do:
if i = 1 then c = entry(i,ctest).
else c = c + "," + entry(i,ctest).
end.
end.

put unformatted c.
 

Stefan

Well-Known Member
Please use code tags for code.

Instead of looping through entries yourself, let the replace function do the work:
Code:
function myReplace returns character (
   i_csource as char,
   i_cfrom   as char,
   i_cto     as char
):

   def var cresult as char no-undo.
   def var csep    as char no-undo initial ','.

   cresult =   replace( 
                  csep + i_csource + csep,
                  csep + i_cfrom   + csep,
                  csep + i_cto + csep 
               ).
   if cresult > '' then
      cresult = substring( cresult, 2, length( cresult ) - 2 ).

   return cresult. 

end function.

def var ctest as char no-undo.

ctest = "demo,demo1,system,test".
message myReplace( ctest, "demo" ,"admin" ).
ctest = "demo,xdemo,system,test".
message myReplace( ctest, "demo" ,"admin" ).

test at: ProgressAblDojo
 
Last edited:
Try the code below to have admin,demo1,system,test
Code:
DEFINE VARIABLE ctest AS CHARACTER   NO-UNDO.

ctest = "demo,demo1,system,test" .
ctest = "," + ctest + "," .
ctest = replace(ctest   ,     ",demo,"     ,      ",admin,"   ).
ctest = TRIM (ctest , "," ).

MESSAGE ctest
    VIEW-AS ALERT-BOX INFORMATION BUTTONS OK

Patrice
 

TomBascom

Curmudgeon
i just use :

ctest = "demo,admin1,system,test"
ctest1 = REPLACE(ctest,"admin1","demo1").

I think you might find that sub-optimal when ctest is something where your target is a substring of other entries. Like this: "demo,admin1,admin12,system,test".
 

PeterB

New Member
How about this:

Code:
DEFINE VARIABLE cList      AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cToBeRepl  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cReplWith  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE iLookup    AS INTEGER     NO-UNDO.

cList       = "demo,demo1,system,test".
cToBeRepl   = "demo".
cReplWith   = "admin".

iLookup = LOOKUP (cToBeRepl, cList).

IF iLookup > 0 THEN
DO:
    ENTRY(iLookup,cList) =  cReplWith.
    
    MESSAGE cList
        VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
END.
 

Stefan

Well-Known Member
How about this:

Code:
DEFINE VARIABLE cList      AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cToBeRepl  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cReplWith  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE iLookup    AS INTEGER     NO-UNDO.

cList       = "demo,demo1,system,test".
cToBeRepl   = "demo".
cReplWith   = "admin".

iLookup = LOOKUP (cToBeRepl, cList).

IF iLookup > 0 THEN
DO:
    ENTRY(iLookup,cList) =  cReplWith.
   
    MESSAGE cList
        VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
END.
Will only replace the first occurrence.
 

PeterB

New Member
No problem.
Code:
DEFINE VARIABLE cList      AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cToBeRepl  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cReplWith  AS CHARACTER   NO-UNDO.

cList       = "demo,demo1,system,test,demo".
cToBeRepl   = "demo".
cReplWith   = "admin".


DO WHILE LOOKUP (cToBeRepl, cList) > 0:
    ENTRY(LOOKUP (cToBeRepl, cList),cList) =  cReplWith.
END.

MESSAGE "Ready: " cList
    VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
 
Top