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

From: Richard Cleis (rcleis at mac.com)
Date: Sat Apr 2 17:20:05 EDT 2011

You can use an index to the string to find the location of your goal, then return the substring when you are done.

rac

On Apr 2, 2011, at 3:08 PM, Charles Hixson wrote:

> 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) ) )
>      (cond
>        [ (= l 0) #f]
>        [ (char-whitespace? (string-ref s (- l 1) ) )    (string-trim (substring s 0 (- l 1) ) ) ]
>        [else s]) ) )
> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users



Posted on the users mailing list.