Next: , Previous: Characters, Up: Top

6 Strings

A string is a mutable sequence of characters. In the current implementation of MIT/GNU Scheme, the elements of a string must all satisfy the predicate char-ascii?; if someone ports MIT/GNU Scheme to a non-ASCII operating system this requirement will change.

A string is written as a sequence of characters enclosed within double quotes " ". To include a double quote inside a string, precede the double quote with a backslash \ (escape it), as in

     "The word \"recursion\" has many meanings."

The printed representation of this string is

     The word "recursion" has many meanings.

To include a backslash inside a string, precede it with another backslash; for example,

     "Use #\\Control-q to quit."

The printed representation of this string is

     Use #\Control-q to quit.

The effect of a backslash that doesn't precede a double quote or backslash is unspecified in standard Scheme, but MIT/GNU Scheme specifies the effect for three other characters: \t, \n, and \f. These escape sequences are respectively translated into the following characters: #\tab, #\newline, and #\page. Finally, a backslash followed by exactly three octal digits is translated into the character whose ISO-8859-1 code is those digits.

If a string literal is continued from one line to another, the string will contain the newline character (#\newline) at the line break. Standard Scheme does not specify what appears in a string literal at a line break.

The length of a string is the number of characters that it contains. This number is an exact non-negative integer that is established when the string is created (but see Variable-Length Strings). Each character in a string has an index, which is a number that indicates the character's position in the string. The index of the first (leftmost) character in a string is 0, and the index of the last character is one less than the length of the string. The valid indexes of a string are the exact non-negative integers less than the length of the string.

A number of the string procedures operate on substrings. A substring is a segment of a string, which is specified by two integers start and end satisfying these relationships:

     0 <= start <= end <= (string-length string)

Start is the index of the first character in the substring, and end is one greater than the index of the last character in the substring. Thus if start and end are equal, they refer to an empty substring, and if start is zero and end is the length of string, they refer to all of string.

Some of the procedures that operate on strings ignore the difference between uppercase and lowercase. The versions that ignore case include `-ci' (for “case insensitive”) in their names.