how to shorten a string from point 'a' to end

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
ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

how to shorten a string from point 'a' to end

Post by ittarter » Thu Mar 24, 2016 4:10 pm

Code: Select all

put "unwanted text : wanted text" into tText; put offset(":",tText) into tNum; put char tNum to last char of tText
Obviously this code doesn't work. "last char" isn't recognized by Livecode. But you get the idea. I know I could do a repeat but I figured there was a simpler way (on one line, I hope?) to do it.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: how to shorten a string from point 'a' to end

Post by Klaus » Thu Mar 24, 2016 4:16 pm

Hi ittarter,

use -1 instead of LAST:
....
# Works:
put "unwanted text : wanted text" into tText; put offset(":",tText) into tNum; put char tNum to -1 of tText
...

Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: how to shorten a string from point 'a' to end

Post by dunbarx » Thu Mar 24, 2016 4:53 pm

Hi.
Obviously this code doesn't work. "last char" isn't recognized by Livecode
Not so. LC can find the "last" almost anything. But do you see why your code cannot?

The last char in a string like "ABCD" is the char "D". It is a chunk. But when you are trying to identify a chunk range, like "char 3 to 5" you cannot use the form you did, something like "char 3 to D"

See? You need to know the difference, since this is a powerful toolset for parsing data strings.

Craig Nemwan

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: how to shorten a string from point 'a' to end

Post by ittarter » Fri Mar 25, 2016 7:46 pm

Thanks, guys.

There's no entry in the dictionary on -1, so I assume that there's a mathematical reason that this works? I've been recommended to use -1 in other bits of code but I've never really understood it so I haven't started using it yet in earnest.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: how to shorten a string from point 'a' to end

Post by FourthWorld » Fri Mar 25, 2016 8:29 pm

In LiveCode chunk expressions positive numbers start counting from the beginning of the string moving forward and negative numbers start from end of the string moving backwards.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: how to shorten a string from point 'a' to end

Post by dunbarx » Sat Mar 26, 2016 2:21 am

Richard said it all.

So if you had a string "ABCDE", and you:

answer char 3 to -1, you get "CDE"
answer char 3 to -3, you get "C"
answer char 3 to -4, you get empty.

It might be said that "the last char" and "char -1" are equivalent (I suspect it is considered more modern to use "-1"). Be aware this only works with chunk expressions. You cannot get the "-1" of fields.

Note that as I mentioned above, LC can get the last almost anything. The last card, field, character, item, whatever. But it does not get the last group in a stable way, at least as of v.6x. Watch out for that...

Craig

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: how to shorten a string from point 'a' to end

Post by ittarter » Sat Mar 26, 2016 9:44 am

FourthWorld wrote:In LiveCode chunk expressions positive numbers start counting from the beginning of the string moving forward and negative numbers start from end of the string moving backwards.
A big light just went on :D

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: how to shorten a string from point 'a' to end

Post by ittarter » Sat Mar 26, 2016 9:47 am

dunbarx wrote:You cannot get the "-1" of fields.
In terms of the "-1" of fields on a card, like, by their ID number? because LC often treats fields as strings, e.g.

Code: Select all

put char 3 to char -1 of fld "text"
would correctly result in the string of fld "text" starting at char 3.

Post Reply