[racket] newbie string handling problem

From: Thomas Chust (chust at web.de)
Date: Thu Mar 31 20:06:53 EDT 2011

2011/4/1 Charles Hixson <charleshixsn at earthlink.net>:
> [...]
> 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,
> [...]

Hello,

well, you can do the same in Racket — just searching for "trim" in the
documentation would have shown you where to find the necessary
functions:

  #lang racket/base
  (require
   srfi/13)

  (provide
   (all-defined-out))

  (define (ends-with-colon? str)
    (string-suffix? ":" (string-trim-right str)))

You could also use a regular expression:

  #lang racket/base
  (provide
   (all-defined-out))

  (define (ends-with-colon? str)
    (regexp-match? #px":\\s*$" str))

By the way, this solution would probably be the more efficient in
Python, as well:

  import re

  def ends_with_colon(str):
    return re.search(':\s*$', str) <> None

I hope that helps :-)

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.



Posted on the users mailing list.