Page 1 of 1
how to shorten a string from point 'a' to end
Posted: Thu Mar 24, 2016 4:10 pm
by ittarter
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.
Re: how to shorten a string from point 'a' to end
Posted: Thu Mar 24, 2016 4:16 pm
by Klaus
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
Re: how to shorten a string from point 'a' to end
Posted: Thu Mar 24, 2016 4:53 pm
by dunbarx
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
Re: how to shorten a string from point 'a' to end
Posted: Fri Mar 25, 2016 7:46 pm
by ittarter
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.
Re: how to shorten a string from point 'a' to end
Posted: Fri Mar 25, 2016 8:29 pm
by FourthWorld
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.
Re: how to shorten a string from point 'a' to end
Posted: Sat Mar 26, 2016 2:21 am
by dunbarx
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
Re: how to shorten a string from point 'a' to end
Posted: Sat Mar 26, 2016 9:44 am
by ittarter
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

Re: how to shorten a string from point 'a' to end
Posted: Sat Mar 26, 2016 9:47 am
by ittarter
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.