s c h e m a t i c s : c o o k b o o k

/ StringChapter / Cookbook.StringRecipeConversionStringVsSymbol

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Converting Between Strings and Symbols

Problem

You want to convert a symbol to a string or vice versa

Solution

The built-in function string->symbol converts from a string to a symbol, and symbol->string does the conversion in the other direction.

For example:

> (string->symbol "hello")
hello
> (symbol->string 'hello)
"hello"

Discussion

Note that since version 299 PLT Scheme is case sensitive, so the symbols 'hello, 'Hello and 'HeLLo' will be different:

> (and (eq? 'hello 'Hello) (eq? 'hello 'HeLLo))
#f

Many other Schemes are case sensitive (though the trend is towards case insensitivity). Using string->symbol is one way of getting around the problem of case-insensitivity when case-sensitive symbols are needed (for example when processing XML):

> (eq? (string->symbol "hello") (string->symbol "Hello"))
#f

Also note that in PLT Scheme there are special quoting rules for symbols that would otherwise be read as something else. For example if you wanted to write a symbol that was bracketed, and so would normally be read as a list, you could write:

> '\(#0000ff\)
|(#0000ff)|
> '|[#0000ff]|
|[#0000ff]|

The symbol doesn't really have the bars or the backslashes in it's name, it's just that it has to be written like that, so the brackets aren't interpreted as lists.

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
TopicOrder: 040

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest