[previous] [up] [next]


6.3.5 Strings

Strings are sequences of characters. Strings are written as sequences of characters enclosed within doublequotes ("). A doublequote can be written inside a string only by escaping it with a backslash (\), as in

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

A backslash can be written inside a string only by escaping it with another backslash. Scheme does not specify the effect of a backslash within a string that is not followed by a doublequote or backslash.

A string constant may continue from one line to the next, but the exact contents of such a string are unspecified.

The length of a string is the number of characters that it contains. This number is an exact, non-negative integer that is fixed when the string is created. The valid indexes of a string are the exact non-negative integers less than the length of the string. The first character of a string has index 0, the second has index 1, and so on.

In phrases such as "the characters of string beginning with index start and ending with index end," it is understood that the index start is inclusive and the index end is exclusive. Thus if start and end are the same index, a null substring is referred to, and if start is zero and end is the length of string, then the entire string is referred to.

Some of the procedures that operate on strings ignore the difference between upper and lower case. The versions that ignore case have "-ci" (for "case insensitive") embedded in their names.

Procedure:  (string? obj)

Returns #t if obj is a string, otherwise returns #f.

Procedure:  (make-string k)
Procedure:  (make-string k char)

Make-string returns a newly allocated string of length k. If char is given, then all elements of the string are initialized to char, otherwise the contents of the string are unspecified.

Procedure:  (string char ...)

Returns a newly allocated string composed of the arguments.

Procedure:  (string-length string)

Returns the number of characters in the given string.

Procedure:  (string-ref string k)

k must be a valid index of string. String-ref returns character k of string using zero-origin indexing.

Procedure:  (string-set! string k char)

k must be a valid index of string. String-set! stores char in element k of string and returns an unspecified value.

  (define (f) (make-string 3 #\*))
  (define (g) "***")
  (string-set! (f) 0 #\?)  =>  unspecified
  (string-set! (g) 0 #\?)  =>  error
  (string-set! (symbol->string 'immutable)
               0
               #\?)  =>  error
Procedure:  (string=? string1 string2)
Procedure:  (string-ci=? string1 string2)

Returns #t if the two strings are the same length and contain the same characters in the same positions, otherwise returns #f. String-ci=? treats upper and lower case letters as though they were the same character, but string=? treats upper and lower case as distinct characters.

Procedure:  (string<? string1 string2)
Procedure:  (string>? string1 string2)
Procedure:  (string<=? string1 string2)
Procedure:  (string>=? string1 string2)
Procedure:  (string-ci<? string1 string2)
Procedure:  (string-ci>? string1 string2)
Procedure:  (string-ci<=? string1 string2)
Procedure:  (string-ci>=? string1 string2)

These procedures are the lexicographic extensions to strings of the corresponding orderings on characters. For example, string<? is the lexicographic ordering on strings induced by the ordering char<? on characters. If two strings differ in length but are the same up to the length of the shorter string, the shorter string is considered to be lexicographically less than the longer string.

Implementations may generalize these and the string=? and string-ci=? procedures to take more than two arguments, as with the corresponding numerical predicates.

Procedure:  (substring string start end)

String must be a string, and start and end must be exact integers satisfying

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

Substring returns a newly allocated string formed from the characters of string beginning with index start (inclusive) and ending with index end (exclusive).

Procedure:  (string-append string ...)

Returns a newly allocated string whose characters form the concatenation of the given strings.

Procedure:  (string->list string)
Procedure:  (list->string list)

String->list returns a newly allocated list of the characters that make up the given string. List->string returns a newly allocated string formed from the characters in the list list, which must be a list of characters. String->list and list->string are inverses so far as equal? is concerned.

Procedure:  (string-copy string)

Returns a newly allocated copy of the given string.

Procedure:  (string-fill! string char)

Stores char in every element of the given string and returns an unspecified value.