Stripping trailing spaces from a string

Ken Ray kray at
Sun Mar 5 01:33:44 EST 2006

On 3/4/06 10:00 PM, "Jim Witte" <jswitte at> wrote:

> How can I use a RegEx to strip trailing white-space?  I tried:
> get matchText(theExpr, "(.*)\s*", theExpr)
> where theExpr is "0025E95C mov      r5, r0                          "

Here's a version I have that trims whitespace from both sides of a string,
and includes 'normal' whitespace, along with hard spaces (watch word wraps):

function trim pWhat
  local dummy,tRetVal
  if the platform is "MacOS" then put "\xca" into tHardSpc
  else put "\xa0" into tHardSpc
  get matchText(pWhat, "(?s)^(\s|" & tHardSpc & ")*(.*?)(\s|" & tHardSpc &
  return tRetVal
end trim

However using RegEx for trimming is not the fastest approach, although the
difference between 0.0391 ticks for the RegEx version vs. 0.0004 ticks for
the fastest version is not recognizable unless you have to do it thousands
of times...

Ken Ray
Sons of Thunder Software
Web site:
Email: kray at

More information about the Use-livecode mailing list