Replace question

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: heatherlaine, Klaus, FourthWorld, kevinmiller

Post Reply
DavJans
Posts: 210
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Replace question

Post by DavJans » Tue Jan 02, 2018 10:36 pm

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.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

quailcreek
Posts: 688
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: Replace question

Post by quailcreek » Tue Jan 02, 2018 11:08 pm

Replace will only replace all of the occurrences.

Try something like this:

Code: Select all

put space into char 4 of tTemp[
Tom
MacBook Pro OS High Sierra 10.13.3

DavJans
Posts: 210
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Replace question

Post by DavJans » Tue Jan 02, 2018 11:39 pm

Perfect solution, thank you.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5278
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Replace question

Post by dunbarx » Wed Jan 03, 2018 3:30 pm

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

DavJans
Posts: 210
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: Replace question

Post by DavJans » Wed Jan 03, 2018 11:44 pm

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 
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5278
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Replace question

Post by dunbarx » Thu Jan 04, 2018 12:36 am

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

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

Re: Replace question

Post by bogs » Thu Jan 04, 2018 2:06 am

<Going to go look for Jacque, she definitely needs to look at what your *cough*{homework}*cough* doing> :twisted:
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1680
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Replace question

Post by [-hh] » Thu Jan 04, 2018 2:35 am

Yet another option.

Code: Select all

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”