Is there an easier way to trim a string?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Is there an easier way to trim a string?

Post by bjb007 » Thu Feb 28, 2008 2:06 pm

The only solution I can find is rather
long-winded.

Simpler code if you can.

http://snipurl.com/20l7i [www_wikifortio_com]
Life is just a bowl of cherries.

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

Re: Is there an easier way to trim a string?

Post by Klaus » Thu Feb 28, 2008 2:41 pm

bjb007 wrote:The only solution I can find is rather
long-winded.
...
As most of your efforts so far :-)

Could you please simply post your code and tell us what you want to achieve next time?
I do not think many people take the time to load your tiny stacks and gues from the scripts what you actually want to do!

OK, maybe this is what you want?
HINT! Please get used to QUOTE names of objects!

Code: Select all

on mouseUp  
  put field "lblTrimString" into theString
  delete item -1 of theString
  put theString & "," into fld "lblTrimString"
end mouseUp

Best

Klaus


P.S.
You should try to stop thinking too complicated!
Don't think C or C++ or whatever!
Think ENGLISH!
Try to tell yourself/wife/girlfirend/buddy/dog/cat what you want to do in english and then you can translate that almost 1:1 into Rev most of the time!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 28, 2008 3:30 pm

You'd be surprised how often I get questions like:

"Hi! I want to delete the last item of a field, but then the comma is also deleted so I want to put a comma after that field!"

while the answer is in the question:

Code:

delete last item of field "Quoted Name"
put comma after field "Quoted Name"


All it requires is structured thinking.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Feb 28, 2008 7:53 pm

Or how about this one-liner?

Code: Select all

put empty into item -1 of x
And slightly more verbose, if you don't like the negative item number syntax

Code: Select all

put empty into the last item of x
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 28, 2008 8:21 pm

Hi Jan,

That's less natural language style and you lose a comma at the end. Surely, the experienced and efficient xTalk programmer does it that way.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Is there an easier way to trim a string?

Post by bjb007 » Thu Feb 28, 2008 9:28 pm

Thanks you all for your suggestions

Yes, it is "thinking reform" that I
need and I'm sure I'll get there in
the end.

Klaus: I find that the best way to get
an answer to a coding problem is to
use a separate script. Posting a bit
of code is more difficult since it's
in a script with different vars etc.

Also think it might be useful to other
learners to have something they can
run "out of the box" rather than trying
to figure out what to do with snippets
they find here.

I beg your indulgence as a nearly-71 year-old
just back from the cardiac ward - with my
"Grump" factor greatly increased.
Life is just a bowl of cherries.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Is there an easier way to trim a string?

Post by bjb007 » Thu Feb 28, 2008 10:02 pm

Done some testing with the ideas offered.

They all suffer from the same fault - OK for
one "Trim" but don't work for repeated
trims.

Perhaps anyone with further suggestions
would like to download the file and test
before posting.
Life is just a bowl of cherries.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri Feb 29, 2008 2:08 am

I don't want to. You demand, but don't even want to tell us what the problem is, nor what solution you'd like, nor what the code is you tried up to now. If you can't even give that, I won't give you what you want.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Is there an easier way to trim a string?

Post by bjb007 » Fri Feb 29, 2008 2:31 am

Already done that. It's all in the
file if you download it.

Shows my code, shows the result I
want, shows everything and all
you have to do is press
a button.

Don't think I can make it any simpler.

If you have a suggestion that will work
you can put your code in the button
script and try it.

Really don't see what your problem is.
Care to tell?
Life is just a bowl of cherries.

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Re: Is there an easier way to trim a string?

Post by Mark Smith » Fri Feb 29, 2008 10:29 am

bjb007 wrote:Done some testing with the ideas offered.

They all suffer from the same fault - OK for
one "Trim" but don't work for repeated
trims.
You need to understand that the engine 'sees' the string as you tell it to, so it can be an item list, or just a string of chars.

So I'd use Jan's one liner, but make it a two liner:

Code: Select all

if char -1 of tString is comma then delete char -1 of tString -- it's a string of chars
put empty into item -1 of tString -- it's an item list
Mark

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

Re: Is there an easier way to trim a string?

Post by Klaus » Fri Feb 29, 2008 1:43 pm

bjb007 wrote:...
Shows my code, shows the result I
want, shows everything and all
you have to do is press
a button...
But no single line of comment what you really want to achieve in the end!

The script shows in as cumbersome way that you want to delete the last number in the field every time you click the button!

Hey, your grumpyness, we are definitively no clairvoyants, although we are working on it ;-)

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Is there an easier way to trim a string?

Post by bjb007 » Fri Feb 29, 2008 2:58 pm

Klaus
Found the "character" function which provides
a more all-purpose way to trim a string.

Also solved the comma-delimited "delete item"
problem by having a comma after the last item
in the field although it's not allowed in programmed operations.
Life is just a bowl of cherries.

ukimiku
Posts: 101
Joined: Thu Oct 22, 2009 10:45 am

Post by ukimiku » Sun Nov 08, 2009 3:08 pm

Code: Select all

replaceText(t, "[ \t]+$", "")
removes spaces and tabs at the end of a string. Remove the "\t" if you only want spaces removed. Regards

Post Reply