[racket] newbie string handling problem

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Mar 31 20:34:50 EDT 2011


Here is what people are talking about when they say do it w/o regular expressions: 

#lang racket

(require srfi/13)

(define (ends-in-: s)
  (define t (string-trim-right s))
  (and (> (string-length t) 0) (char=? (string-ref t (- (string-length t) 1)) #\:)))

(ends-in-: "abc")
(ends-in-: " abc:  ")
(ends-in-: "   ")


On Mar 31, 2011, at 7:38 PM, 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?
> 
> 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.  (Of course, part of it is that the best way I've figured to proceed from there is to then copy that list to another list checking at each step of the way to tell if I was done, and then if it was successful to join the resultant list back into a string.)
> _________________________________________________
> For list-related administrative tasks:
> http://lists.racket-lang.org/listinfo/users




Posted on the users mailing list.