[racket] newbie string handling problem

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Mar 31 19:46:01 EDT 2011

Two minutes ago, Charles Hixson wrote:
> Hi, I'm trying to determine whether the last non-whitespace
> character of a string is a colon, and I haven't been able to figure
> out the best way of doing this.  In python I'd just trim the string
> and look at the character before the end, but while Racket seems to
> have a string-length function, it doesn't seem to have anything
> analogous to trim.  So how should it be done?

Try this:

  (define (last-non-ws str)
    (cond [(regexp-match #px"(\\S)\\s*$" " \t") => cadr]
          [else #f]))

then see the docs about regexps and this use of `cond'.


> The best I've come up with is to explode the string into a list of
> characters, and handle it that way, but that's clearly quite
> wasteful of both processing and RAM.

(Right -- and IIRC, it's also a problem with trimming the string.)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.