string-ref - get a character from a string
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
(string-ref string k)
using zero-origin indexing.
Implementors should make
run in constant time.
There is no requirement for this procedure to execute in constant
- Chibi Scheme
Chibi supports O(1) string access if the
feature is enabled.
Returns a single value which is a character object. It is
to the one that was last placed in the string at index
(string-ref "abc" 1) => #\b
This procedure is used to implement algorithms that read strings in
non-linear access patterns. Programs that read strings at increasing
indices can use
The choice of R7RS to not require execution in constant time opens the
possibility for implementations to use UTF-8 or UTF-16 as the internal
This procedure is functionally the same in all RnRS revisions after
R2RS. The differences are in which characters an implementation can
represent, how errors are signalled, and the time complexity of doing
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain.
is not a valid index of
The assertions described above are errors.
Implementations may signal an error, extend the procedure's
domain of definition to include such arguments,
or fail catastrophically.
First introduced in R2RS. Scheme before R2RS running on MacLISP had a
function, but it used one-origin indexing and worked on atomic symbols
instead of character strings.
This page is part of the
It includes materials from the RnRS documents.
More information can be found at
It is easy to get the index validity check wrong.
GNU Guile 3.0.8 segfaults on evaluating (string-ref "" -1).
Markup created by unroff 1.0sc, March 04, 2023.