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

/ StringRecipes? / Sandbox.StringBasics

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

2.2.1. String Basics

In Scheme, literal strings are enclosed in quotes. According to R5RS, "A string constant may continue from one line to the next, but the exact contents of such a string are unspecified.", probably to account for platform differences in handling newlines. The PLT and mzScheme interpreters allow you to write string literals with linebreaks inline, i.e. 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

R5RS provides the string->list and list->string functions to convert between strings and lists: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

If you want to find out what character is at a specific position in a string, use (string-ref string *i*), where i is a 0-based index in the string: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

string-length will tell you how long a string is. 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

You may have heard that Scheme provides mutable strings. If you want to modify a string in place, the string-set! function will do that for you. However, if you gave the string as a literal to the REPL, string-set! will give you a surprise: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

You can make a copy of a string using string-copy. This will yield a mutable string: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

You can allocate a new string with make-string. make-string comes in 2 forms, the second allows you to specify an initial character: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

You can also use string-append to join strings. This 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

R5RS provides a number of useful string functions, but you should also be aware of SRFI 13 (String Library) and SRFI-14 (Character Set Library), which add a number of very useful functions.

-- GordonWeakliem - 05 Apr 2004

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