Strings

Course links

A string is a sequence of zero or more characters. Most strings can be named by enclosing the characters they contain between double quotation marks, to produce a string literal: for instance, "hyperbola" is the nine-character string consisting of the characters #\h, #\y, #\p, #\e, #\r, #\b, #\o, #\l, and #\a, in that order, and "" is the zero-character string (the null string).

String literals may contain spaces and newline characters; when such characters are between double quotation marks, they are treated like any other characters in the string. There is a slight problem when one wants to put a double quotation mark into a string literal: To indicate that the double quotation mark is part of the string (rather than marking the end of the string), one must place a backslash character immediately in front of it. For instance, "Say \"hi\"" is the eight-character string consisting of the characters #\S, #\a, #\y, #\space, #\", #\h, #\i, and #\", in that order. The backslash before a double quotation mark in a string literal is an escape character, present only to indicate that the character immediately following it is part of the string.

This use of the backslash character causes yet another slight problem: What if one wants to put a backslash into a string? The solution is to place another backslash character immediately in front of it. For instance, "a\\b" is the three-character string consisting of the characters #\a, #\\, and #\b, in that order. The first backslash in the string literal is an escape, and the second is the character that it protects, the one that is part of the string.

Scheme provides several basic procedures for working with strings:

The principal author of this reading is Professor Henry Walker. I am also indebted to Professor Ben Gum for his contributions to its development and to Professor Samuel Rebelsky for pointing out a typographical error in a previous version.