Page 1 of 1

Replace question

Posted: Tue Jan 02, 2018 10:36 pm
by DavJans
I need to replace only the last occurrence of - in a string but am not having any luck.

Example;
put 1-2-3 into tTemp
replace character 4 with " " in tTemp

That doesn't work

replace the last "-" with " " in tTemp

Doesn't work either.

I don't know what else to try. Thank you in advance.

Re: Replace question

Posted: Tue Jan 02, 2018 11:08 pm
by quailcreek
Replace will only replace all of the occurrences.

Try something like this:

Code: Select all

put space into char 4 of tTemp[

Re: Replace question

Posted: Tue Jan 02, 2018 11:39 pm
by DavJans
Perfect solution, thank you.

Re: Replace question

Posted: Wed Jan 03, 2018 3:30 pm
by dunbarx
Hi.

Is your data always such that the fourth char is the one that needs replacing?

If not, you could set up a repeat loop:

Code: Select all

on mouseUp
   repeat with y = the number of chars of yourText down to 1
      if char y of yourText = "-" then
         put space into char y of yourText
         exit repeat
      end if
   end repeat
end mouseUp
Now the last "-" is replaced, wherever it lies.

Craig Newman

Re: Replace question

Posted: Wed Jan 03, 2018 11:44 pm
by DavJans
Thnx Craig,

You are correct that its not always the 4th character. Here is my code, but I like yours better.

Code: Select all

         put the length of sandl into tLength
         put "1" into tTimes
         repeat until tTimes > tLength
            if character tTimes in sandl = "-" then
               put tTimes into lastHyphen
            end if
            put tTimes + 1 into tTimes
         end repeat
         put " " into char lastHyphen of sandl 

Re: Replace question

Posted: Thu Jan 04, 2018 12:36 am
by dunbarx
So do I. :D

My handler only addresses a single line, right? It exits when it finds the lastMost hyphen.

Don't tell Jacque, but can you post a modified handler that loses the lastMost hyphen in each line, and skips those that do not need processing?

Craig

Re: Replace question

Posted: Thu Jan 04, 2018 2:06 am
by bogs
<Going to go look for Jacque, she definitely needs to look at what your *cough*{homework}*cough* doing> :twisted:

Re: Replace question

Posted: Thu Jan 04, 2018 2:35 am
by [-hh]
Yet another option.

Code: Select all

set the itemdelimiter to "-"
put (item 1 to -2 of sandl) & space & (item -1 of sandl) into sandl