s c h e m e w i k i . o r g

/ SearchResult? / Sandbox.IsAString

This Web


WebHome 
WebChanges 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
TWiki Shorthand

Offsite Links


Schemers.org
Lambda the Ultimate
twiki.org

Testing if an Object is a String

Problem

You need to test if an object is a string.

Solution

The string? predicate is what you want:
> (string? "foo")
#t
> (string? #\f)
#f
> (string? (list #\f #\o #\o))
#f
Notice that a character is not a string. Also, even though we've said that a string is a list of characters, a list of characters is not a string.

-- GordonWeakliem - 05 Apr 2004 The Python Cookbook discusses how simplay asking isinstance is "UnPythonic" and has a discussion on "duck typing", in that their solution just asks if you can concatenate a string to an unknown object. I'm not sure how to address this. You could literally ask if (string-append an-object "") works w/o throwing an exception, but would anyone do this?

(define (is-string? s)
    (with-handlers ((not-break-exn?
                   (lambda (exn) 
                     #f)))
    (let ((s2 (string-append s ""))) #t)))

 
 
© 2004 by the contributing authors. / You are Main.guest