How trim?

Dave Cragg dcragg at lacscentre.co.uk
Sun Oct 23 03:12:00 EDT 2005


Is there a requirement for the solutions to be one liners?

I think the following 2 are faster than any solutions using  
replaceText, and more readable too.

function trimL pString
   put " " & tab & return into x
   repeat while char 1 of pString in in x
     delete char 1 of pString
   end repeat
   return pString
end trimL

function trimR pString
   put " " & tab & return into x
   repeat while char -1 of pString in in x
     delete char -1 of pString
   end repeat
   return pString
end trimR

The following is not as fast as "return word 1 to -1 of ....", but  
it's less than twice as slow. And it makes it easy to modify which  
whitespace characters to remove.


function trim pString
   put " " & tab & return into x
   repeat while char 1 of pString in in x
     delete char 1 of pString
   end repeat
   repeat while char -1 of pString in in x
     delete char -1 of pString
   end repeat
   return pString
end trim


Cheers
Dave



More information about the use-livecode mailing list