Page 1 of 1

[SOLVED] Problem with replacement of a character

Posted: Sat May 02, 2020 10:58 am
by redfield
Hi Livecoders,
probably this is a very simple one, but unfortunately not simple enough for me :? . In a string, e. g. "/folder/subfolder", I am wanting to replace the second "/" with a "."

To achieve this I coded the following:

Code: Select all

put "/folder/subfolder" into fld "fField"
     if offset("/", fld "fField", 1) > 1 then
        put offset("/", fld "fField", 1) +1 into charPosition
        replace char charPosition of fld "fField" with "." in fld "fField"
     end if
With the above code though, both "/" are turned into a "." and not only the second one. How come :shock: ?

Re: Problem with replacement of a character

Posted: Sat May 02, 2020 11:32 am
by bogs
The problem is that your using 'replace', which replaces everything in that field.
Dictionary wrote: Use the replace command to replace all instances of one string with another string.
Instead, try "put", i.e.

Code: Select all

   put "/folder/subfolder" into fld 1
   put offset("/", fld 1, 1) into tVar
     if offset("/", fld 1, 1) > 1 then
        put offset("/", fld 1, 1) +1 into charPosition
        -- replace char charPosition of fld 1 with "." in fld 1
        put "." into char charPosition of fld 1
     end if

Re: [SOLVED] Problem with replacement of a character

Posted: Sat May 02, 2020 5:26 pm
by redfield
Okay thanks. I thought by defining the position of the char to be replaced ("... char charPosition..."), only this one position would be touched. AND I forgot all about "put into". Works fine now.