Page 1 of 1

Trim string function

Posted: Sep 12th, '17, 14:49
by SimpleSi
I'm finding I could do with a simple trim function to remove leading and trailing spaces from strings

My workaround is a bit messy and long (and only deals with one space at either end)
0.png

Re: Trim string function

Posted: Sep 14th, '17, 20:41
by JohnM
GP already has a trim function, it's just not in the palette. However, if you enter developer mode, you can open a "system palette", select the "String" class (a good place to look for handy string utilities), and find the "trim" command (bottom of second column):
Screen Shot 2017-09-14 at 4.35.37 PM.png
Once you know it exists, the easiest way to get a "trim" block is to open a workspace window, type "trim 'foo'", select that text, and invoke the "blockify" command from the menu. You can also drag blocks directly from the system palette, but you often don't get a block editable input slots.

Another way you might find it, if you don't mind using the command line, is to use the Unix/Linux "grep" command to search for the string "trim" in the GP library folder:

Code: Select all

> grep trim runtime/lib/*
runtime/lib/BlockSpec.gp:    atPut specs i (trim (at specs i))
runtime/lib/String.gp:method trim String {
The second line of the results shows that "trim" is a method defined in the class String, so it's an operation you can use on strings.

There should be easier ways to find useful operations like "trim".

Re: Trim string function

Posted: Sep 15th, '17, 20:33
by SimpleSi
Thanks for info
and
There should be easier ways to find useful operations like "trim".
yes :)
Ok for those of us who like diving deep but newcommers with arm bands need something easier :)