Page 1 of 1

Is there an easier way to trim a string?

Posted: Thu Feb 28, 2008 2:06 pm
by bjb007
The only solution I can find is rather
long-winded.

Simpler code if you can.

http://snipurl.com/20l7i [www_wikifortio_com]

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

Posted: Thu Feb 28, 2008 2:41 pm
by Klaus
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!

Posted: Thu Feb 28, 2008 3:30 pm
by Mark
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

Posted: Thu Feb 28, 2008 7:53 pm
by Janschenkel
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.

Posted: Thu Feb 28, 2008 8:21 pm
by Mark
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

Is there an easier way to trim a string?

Posted: Thu Feb 28, 2008 9:28 pm
by bjb007
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.

Is there an easier way to trim a string?

Posted: Thu Feb 28, 2008 10:02 pm
by bjb007
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.

Posted: Fri Feb 29, 2008 2:08 am
by BvG
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.

Is there an easier way to trim a string?

Posted: Fri Feb 29, 2008 2:31 am
by bjb007
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?

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

Posted: Fri Feb 29, 2008 10:29 am
by Mark Smith
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

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

Posted: Fri Feb 29, 2008 1:43 pm
by Klaus
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 ;-)

Is there an easier way to trim a string?

Posted: Fri Feb 29, 2008 2:58 pm
by bjb007
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.

Posted: Sun Nov 08, 2009 3:08 pm
by ukimiku

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