[SOLVED] Problem with replacement of a character

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

[SOLVED] Problem with replacement of a character

Post by redfield » Sat May 02, 2020 10:58 am

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: ?
Last edited by redfield on Sat May 02, 2020 5:22 pm, edited 1 time in total.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Problem with replacement of a character

Post by bogs » Sat May 02, 2020 11:32 am

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
Image

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: [SOLVED] Problem with replacement of a character

Post by redfield » Sat May 02, 2020 5:26 pm

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.

Post Reply