Scheme strings
last modified November 24, 2026 Guile 3.0
A string is a sequence of characters. The
data types article showed string literals,
string-length, string-ref, and
string-append. This article covers the rest of the everyday
string toolkit: creating, cutting, joining, comparing, searching, splitting,
and converting strings. R7RS defines a small set of string procedures, and
Guile adds the larger SRFI 13 library to its core, so procedures such as
string-join and string-contains need no import. The
examples run with GNU Guile 3.0.7.
Literals and escapes
A string literal is written in double quotes. A backslash starts an escape
sequence: \" is a quote, \\ a backslash,
\t a tab, and \n a newline. In Guile,
\u followed by four hexadecimal digits inserts a Unicode
character.
(define path "C:\\temp\\notes.txt")
(define quoted "She said \"Hello there\".")
(define columns "name\tage\tcity")
(define greek "\u03bb is lambda")
(for-each (lambda (s)
(write s)
(newline)
(display s)
(newline))
(list path quoted columns greek))
(display "Žilina, Košice, Prešov")
(newline)
(display (string-length "Žilina"))
(newline)
(display "first line
second line")
(newline)
write prints each string in the form that would read back as the
same string, with the escapes, and display prints the characters
themselves. λ is the Greek letter lambda. Guile's
\x escape takes exactly two hex digits, so the R7RS form
\x3bb; would read as a semicolon followed by b;.
Letters with diacritics can be typed directly, and string-length
counts characters, not bytes: "Žilina" has six. A literal may also span several
lines; the line break becomes part of the string.
$ guile literals.scm "C:\\temp\\notes.txt" C:\temp\notes.txt "She said \"Hello there\"." She said "Hello there". "name\tage\tcity" name age city "λ is lambda" λ is lambda Žilina, Košice, Prešov 6 first line second line
Creating strings
Besides literals, strings are created from characters, from lists of
characters, and from other values. make-string repeats one
character, and string joins any number of characters.
(write (make-string 5 #\*)) (newline) (write (string #\S #\c #\h #\e #\m #\e)) (newline) (write (list->string (list #\o #\k))) (newline) (write (number->string 3.14)) (display " ") (write (symbol->string 'planet)) (newline) (write (string)) (display " ") (write (string-length "")) (display " ") (write (string-null? "")) (newline) (define original "draft") (define copy (string-copy original)) (write (list original copy (equal? original copy) (eq? original copy))) (newline)
list->string builds a string from a list of characters, and
number->string and symbol->string convert
other values. (string) with no arguments returns the empty
string, which string-null? recognises. string-copy
creates a new string with the same characters: equal? sees the
same contents, while eq? shows that the two are different
objects.
$ guile creating.scm
"*****"
"Scheme"
"ok"
"3.14" "planet"
"" 0 #t
("draft" "draft" #t #f)
Substrings
Characters are indexed from zero. substring takes a start index
and an end index, and returns the characters from the start up to, but not
including, the end. In Guile the end may be left out.
(define city "Bratislava")
(display (string-length city))
(newline)
(write (string-ref city 0))
(display " ")
(write (string-ref city (- (string-length city) 1)))
(newline)
(write (substring city 0 5))
(display " ")
(write (substring city 5))
(newline)
(write (substring city 3 3))
(newline)
(define (first-chars s n)
(substring s 0 (min n (string-length s))))
(define (last-chars s n)
(let ((len (string-length s)))
(substring s (max 0 (- len n)) len)))
(write (list (first-chars city 4) (first-chars "ok" 4)))
(newline)
(write (list (last-chars city 4) (last-chars "ok" 4)))
(newline)
The last character is at index (- (string-length city) 1).
(substring city 0 5) returns the first five characters, and
(substring city 5) the rest. Equal start and end give the empty
string. Indices beyond the string are an error, so first-chars
and last-chars clamp them with min and
max and work for short strings too.
$ guile substrings.scm
10
#\B #\a
"Brati" "slava"
""
("Brat" "ok")
("lava" "ok")
Joining strings
string-append joins its arguments. For a list of strings,
string-concatenate does the same, and string-join
puts a delimiter between the elements.
(define words '("red" "green" "blue"))
(write (string-append "red" "-" "green"))
(newline)
(write (apply string-append words))
(newline)
(write (string-concatenate words))
(newline)
(write (string-join words))
(newline)
(write (string-join words ", "))
(newline)
(write (string-join words ";" 'suffix))
(newline)
(write (string-join words "/" 'prefix))
(newline)
(write (string-join '() ", "))
(newline)
(define (make-csv-line fields)
(string-join (map (lambda (x) (if (number? x) (number->string x) x))
fields)
","))
(display (make-csv-line (list "Lucia" 34 "Bratislava")))
(newline)
(apply string-append words) and
string-concatenate produce the same result. Without a delimiter,
string-join uses a space. The optional grammar argument controls
where the delimiter goes: the default infix puts it between
elements, suffix after each one, and prefix before
each one. Joining the empty list gives the empty string.
make-csv-line converts the numbers to strings before joining the
fields with commas.
$ guile joining.scm "red-green" "redgreenblue" "redgreenblue" "red green blue" "red, green, blue" "red;green;blue;" "/red/green/blue" "" Lucia,34,Bratislava
Comparing strings
string=? compares the characters of strings, and
string<?, string>?, and their relatives order
them lexicographically. The -ci variants ignore case. All of them
accept more than two arguments.
(define a "apple")
(define b (string-append "app" "le"))
(write (list (string=? a b) (equal? a b) (eq? a b)))
(newline)
(write (list (string<? "apple" "banana") (string<? "apple" "app")))
(newline)
(write (string<? "Zebra" "apple"))
(newline)
(write (list (string=? "Hello" "hello") (string-ci=? "Hello" "hello")))
(newline)
(write (string<? "a" "b" "c" "d"))
(display " ")
(write (string=? "x" "x" "y"))
(newline)
(define (shorter? s1 s2)
(< (string-length s1) (string-length s2)))
(write (list (shorter? "fig" "plum") (string<? "fig" "plum")
(shorter? "kiwi" "fig") (string<? "kiwi" "fig")))
(newline)
Two strings with the same characters are string=? and
equal? but not eq?; the second one is built at run
time, because the compiler would otherwise merge equal literals. A prefix
sorts before the longer string, so "app" is smaller than
"apple". Characters are compared by code point, and all uppercase
letters come before the lowercase ones: "Zebra" sorts before
"apple". With several arguments, string<? checks
that they are in increasing order. The last line shows that lexicographic
order is not the same as ordering by length.
$ guile comparing.scm (#t #t #f) (#t #f) #t (#f #t) #t #f (#t #t #f #f)
Changing case
string-upcase and string-downcase convert every
letter, and Guile's string-titlecase capitalises the first letter
of every word. All of them return new strings.
(define title "the old man and the SEA")
(write (string-upcase title))
(newline)
(write (string-downcase title))
(newline)
(write (string-titlecase title))
(newline)
(write (string-upcase "Žilina, Čadca"))
(newline)
(write (string-upcase "straße"))
(newline)
(define (capitalize s)
(if (string-null? s)
s
(string-append (string-upcase (substring s 0 1))
(string-downcase (substring s 1)))))
(write (map capitalize '("bRATISLAVA" "x" "")))
(newline)
The conversions handle letters with diacritics such as Ž and Č. They map each
character to one character, so the German ß stays as it is instead of
becoming SS. capitalize uppercases the first character, lowercases
the rest, and returns the empty string unchanged, because
(substring s 0 1) would fail on it.
$ guile case.scm
"THE OLD MAN AND THE SEA"
"the old man and the sea"
"The Old Man And The Sea"
"ŽILINA, ČADCA"
"STRAßE"
("Bratislava" "X" "")
Searching
string-index returns the index of the first occurrence of a
character and string-rindex that of the last.
string-contains searches for a substring.
string-prefix? and string-suffix? test the
beginning and the end. All return #f when there is no match.
(define text "the rain in Spain stays mainly in the plain")
(write (string-index text #\a))
(display " ")
(write (string-rindex text #\a))
(display " ")
(write (string-index text #\z))
(newline)
(write (string-contains text "ain"))
(display " ")
(write (string-contains text "snow"))
(newline)
(write (list (string-prefix? "the" text) (string-suffix? "plain" text)))
(newline)
(write (string-contains-ci text "SPAIN"))
(newline)
(define (count-occurrences s part)
(let loop ((start 0) (count 0))
(let ((pos (string-contains s part start)))
(if pos
(loop (+ pos 1) (+ count 1))
count))))
(write (count-occurrences text "ain"))
(display " ")
(write (count-occurrences "aaaa" "aa"))
(newline)
The first a is at index 5 and the last one at 40. string-contains
returns the index where "ain" starts, and
string-contains-ci ignores case. Note the argument order of
string-prefix?: the prefix comes first. string-contains
accepts a start index, which count-occurrences uses to continue
the search one character after each match. Because it moves on by one
character only, the matches may overlap: "aa" occurs three times
in "aaaa".
$ guile searching.scm 5 40 #f 5 #f (#t #t) 12 4 3
Splitting
Guile's string-split cuts a string at every occurrence of a
delimiter character and returns a list of the pieces. It is the inverse of
string-join with the same delimiter.
(define line "Lucia,34,Bratislava,Slovakia") (define fields (string-split line #\,)) (write fields) (newline) (write (length fields)) (display " ") (write (string->number (cadr fields))) (newline) (write (string-split "a,b,,c," #\,)) (newline) (write (string-split "one two three" #\space)) (newline) (write (string-split "no-separator" #\,)) (display " ") (write (string-split "" #\,)) (newline) (define path "/usr/local/share/guile") (write (string-split path #\/)) (newline) (write (string-join (cdr (string-split path #\/)) " > ")) (newline)
A line of comma-separated fields becomes a list of four strings, and
string->number converts the age. Every delimiter produces a
cut, so two commas in a row, a trailing comma, or two spaces give empty
strings. A string without the delimiter comes back as a one-element list. A
path that starts with / yields an empty first element, which
cdr drops before the parts are joined again with a different
separator. Splitting on several delimiters and dropping empty fields is
covered in the string processing
article.
$ guile splitting.scm
("Lucia" "34" "Bratislava" "Slovakia")
4 34
("a" "b" "" "c" "")
("one" "two" "" "three")
("no-separator") ("")
("" "usr" "local" "share" "guile")
"usr > local > share > guile"
Conversions
string->list turns a string into a list of characters, which
the list procedures can process, and list->string turns it
back. string->number parses numbers, and
number->string formats them, both with an optional radix.
(write (string->list "Scheme"))
(newline)
(write (list->string (reverse (string->list "Scheme"))))
(newline)
(write (string-reverse "stressed"))
(newline)
(write (list (string->number "42") (string->number "-3.5")
(string->number "2/3") (string->number "1e3")))
(newline)
(write (list (string->number "ff" 16) (string->number "1010" 2)
(string->number "12abc")))
(newline)
(write (list (number->string 255 16) (number->string 255 2)
(number->string 1/3)))
(newline)
(write (string->symbol "blue"))
(display " ")
(write (eq? (string->symbol "blue") 'blue))
(newline)
Reversing the character list reverses the string; Guile also has
string-reverse for that. string->number accepts
every number syntax, including rationals and exponents, and returns
#f for text that is not a complete number, such as
"12abc". With a radix of 16 or 2 it parses hexadecimal and binary
digits, and number->string writes numbers in those bases.
string->symbol returns the symbol with the given name, the
same object as the quoted 'blue.
$ guile converting.scm
(#\S #\c #\h #\e #\m #\e)
"emehcS"
"desserts"
(42 -3.5 2/3 1000.0)
(255 10 #f)
("ff" "11111111" "1/3")
blue #t
Iterating over characters
string-for-each calls a procedure on every character, and
string-map builds a new string from the characters that the
procedure returns. string-count counts the occurrences of a
character.
(define word "Mississippi")
(string-for-each (lambda (c)
(write c)
(display " "))
"abc")
(newline)
(write (string-map (lambda (c) (if (char=? c #\space) #\_ c)) "snake case name"))
(newline)
(write (string-map (lambda (c) (if (char=? c #\s) #\z c)) word))
(newline)
(write (string-count word #\s))
(newline)
(define (spaced s)
(let loop ((i 0) (parts '()))
(if (= i (string-length s))
(string-join (reverse parts) " ")
(loop (+ i 1) (cons (string (string-ref s i)) parts)))))
(write (spaced "GUILE"))
(newline)
write prints the characters in their #\ literal
form. The first string-map replaces spaces with underscores and
the second replaces every s with z, comparing characters with
char=?. spaced shows the manual approach: a loop over
the indices collects one-character strings, which are joined with spaces at
the end.
$ guile iterating.scm #\a #\b #\c "snake_case_name" "Mizzizzippi" 4 "G U I L E"
Mutable strings
string-set! replaces the character at an index and
string-fill! sets all characters. They work only on mutable
strings, such as those made by make-string and
string-copy. Literal strings are constants, and Guile enforces
this.
(define buffer (make-string 6 #\-)) (string-set! buffer 0 #\[) (string-set! buffer 5 #\]) (write buffer) (newline) (define name (string-copy "maria")) (string-set! name 0 #\M) (write name) (newline) (string-fill! buffer #\=) (write buffer) (newline) (define label "fixed") (string-set! label 0 #\F) (write label) (newline)
The buffer from make-string gets brackets at both ends, the copy
of "maria" gets a capital letter, and string-fill!
overwrites the whole buffer. The last string-set! tries to change
the literal "fixed", and Guile stops with an error. Unlike
set-car! on a quoted list, which Guile 3.0.7 lets pass, writing to
a literal string is always detected. The backtrace is left out.
$ guile mutable.scm "[----]" "Maria" "======" mutable.scm:17:0: string is read-only: "fixed"
Source
Strings — GNU Guile Reference Manual
R7RS: the Revised7 Report on Scheme
In this article we have written string literals with escapes, created strings from characters and other values, cut substrings, joined strings with and without delimiters, compared them with and without case, changed their case, searched for characters and substrings, split strings at a delimiter, converted them to lists, numbers, and symbols, iterated over their characters, and changed mutable strings in place. The characters article covers the elements of strings in more detail.
Author
List all Scheme tutorials.