[racket] string-trim : an implementation & a question

From: Charles Hixson (charleshixsn at earthlink.net)
Date: Sat Apr 2 17:08:19 EDT 2011

This seems to be what I want the string-trim to do, but it seems that 
all the string copying would be expensive.  Is there a way to improve it 
by avoiding the string copying?

My original inclination was to use a while loop with a test for 
non-whitespace, but that appears to not be something scheme supports.

(define (string-trim s)
     (let ( (l (string-length s) ) )
         [ (= l 0) #f]
         [ (char-whitespace? (string-ref s (- l 1) ) )    (string-trim 
(substring s 0 (- l 1) ) ) ]
         [else s]) ) )

Posted on the users mailing list.