t3x.org / sketchy / library / substring.html
SketchyLISP
Reference
  Copyright (C) 2006
Nils M Holm

substring

Conformance: R5RS

Purpose: Extract a substring from a string. The extracted string begins at the position start and extends to (but does not include) the char at the position end. The assertion 0<=start<=end must hold.

Arguments:
STR - string
START - position of first char to extract
END - position of first char not to extract

Model:

(define (substring str start end)
  (letrec
    ((skip (lambda (lst n)
      (cond ((zero? n) lst)
        ((null? lst)
          (bottom '(bad start in substring)))
        (#t (skip (cdr lst) (- n 1))))))
    (extract (lambda (lst n res)
      (cond ((zero? n) (reverse res))
        ((null? lst)
          (bottom '(bad end in substring)))
        (#t (extract (cdr lst) (- n 1)
              (cons (car lst) res)))))))
    (cond ((< end start)
        (bottom '(bad range in substring)))
      (#t (list->string
            (extract (skip (string->list str) start)
              (- end start) '()))))))

Implementation:

; This function is a primitive function.

Example:

(substring "abcdefxyz" 3 6) 
=> "def"

See also:
string, string-length, string-ref, string-append.