Strings
2010-12-11
Introduction
Strings are sequences of characters. They are entered into a program between double quotes, e.g. "Hello". Scheme has a few fundamental procedures for manipulating strings, as well as procedures that convert between strings and other types.
To create a string that has the double quote character in it, escape it with a backslash. E.g. "He said: \"Hello!\"". To insert a literal
backslash, double it. E.g. "backslash: \\". Many Scheme implementations support full C-style escapes, e.g. \n
for newline, but this is
not specified by the standard and it's not generally a good idea to rely on this feature.
Operations on Strings
; Test if something is a string
(string? "hello") ; => #t
(string? 'hello) ; => #f
; Compare strings
(string=? "hello" "hello") ; => #t
(string=? "hello" "Hello") ; => #f
(string<? "hello" "hello") ; => #f
(string<? "hello" "Hello") ; => #f
(string>? "hello" "hello") ; => #f
(string>? "hello" "Hello") ; => #t
(string<=? "hello" "hello") ; => #t
(string<=? "hello" "Hello") ; => #f
; Case insensitive comparison
(string-ci=? "hello" "Hello") ; => #t
(string-ci>? "hello" "Hello") ; => #f
; Return a copy of a string
(string-copy "hello") ; => "hello"
; Return a copy of part of a string
(substring "Hello" 1 3) ; => "el"
; Return the character at the specified position
(string-ref "hello" 4) ; => #\o
; Concatenate strings
(string-append "Hello" ", " "World" "!") ; => "Hello, World!"
; Make a string of a certain length
(make-string 12) ; returns a string of 12 characters
(make-string 12 "-") ; => "------------"