>Is there a RunRev TRIM command? To remove beginning and/or trailing spaces.
>If there is, I can't find it.
>If there isn't, how would I script the function for this?
AFAIK, there isn't.
If this is *within* a line (or if you don't need to worry about CR for
some other reason), you can just do
word 1 to -1 of tVar
If you need to worry about multi-line, you need to be careful (but most
TRIM functions don't, so I'm going to hope you don't either :-)
On 5/19/06, [hidden email] <[hidden email]> wrote:
> Hi all,
> Is there a RunRev TRIM command? To remove beginning and/or trailing spaces.
> If there is, I can't find it.
> If there isn't, how would I script the function for this?
There was a lot of discussion about this a few months ago, so an
archive search might prove useful. However here are a couple of
suggestions to get you going:
If the extra white spaces are only at the beginning & end of the text
i.e. not between words, then this one-liner will do:
put word 1 to -1 of myText into myText
If there could be extra spaces between words, then this is what I use:
put empty into myTrimmedText
repeat for each word w in myText
put w & space after myTrimmedText
delete last char of myTrimmedText -- get rid of last space
Salery is the easiest way to get your own web store on-line: http://
Op 18-mei-2006, om 23:05 heeft Sarah Reichelt het volgende geschreven:
> If there could be extra spaces between words, then this is what I use:
> put empty into myTrimmedText
> repeat for each word w in myText
> put w & space after myTrimmedText
> end repeat
> delete last char of myTrimmedText -- get rid of last space
> I don't claim to be a regular expression expert (just a long time
> list lurker), but I recently had the same need for a trim command and
> came up with this function:
> function trimWS pText
> -- Remove all leading and trailing whitespace characters.
> -- I.E., space, tab, carriage return, line feed, form feed.
> return replaceText(pText,"(^\s+)|(\s+$)",empty)
> end trimWS
Yes, this works for everything except hard spaces (i.e. nonbreaking spaces),
which are not seen as whitespace by the regex engine. Admittedly this
should be extremely rare that you might find one of these, but if you do, it
can ruin your day. Here's a version that takes this into account
(unfortunately it is different per platform):
function trimWS pText
-- Remove all leading and trailing whitespace characters.
-- I.E., space, tab, carriage return, line feed, form feed, hard space
if the platform is "MacOS" then
For those of you interested, I've been keeping track of the various forms of
trimming, and have (with RevBench's help) shown that although the above uses
less code, it is not as efficient as a much more wordy function (the
function above comes in at 0.0257 ticks).
The following trim function is so far the fastest, most comprehensive
version (0.0004 ticks):
function trim what
if the platform is "MacOS" then put numToChar(202) into tHardSpc
else put numToChar(160) into tHardSpc
put (space & tab & cr & linefeed & formfeed & tHardSpc) into tWhiteSpc
if char 1 of what is not in tWhiteSpc then exit repeat
else delete char 1 of what
if char -1 of what is not in tWhiteSpc then exit repeat
else delete char -1 of what
However the approach provided by Jacque Gay (and others) is the simplest and
is fastest (0.0002 ticks):
function trim what
return (word 1 to -1 of what)
but doesn't handle hard spaces and is tripped up if there is a lone double
quote in a string:
put quote & " this is a test " into tString
will *not* trim the spaces from the end of the string.