strip

Name

strip -- Strip leading and trailing characters off of a string

Synopsis

(strip str #!optional (stripchars '(#\space #\
 #\U-0009)))

Description

Strips leading and trailing characters in the stripchars list off of a string and returns the stripped string.

str

The string to strip

stripchars

A list of characters that should be stripped.

Author

Norman Walsh, <ndw@nwalsh.com>

Source Code

(define (strip str #!optional (stripchars '(#\space #\
 #\U-0009)))
  ;; Strip leading and trailing characters off of a string
  (let* ((startpos (let loop ((count 0))
		     (if (>= count (string-length str))
			 (string-length str)
			 (if (member (string-ref str count) stripchars)
			     (loop (+ count 1))
			     count))))
	 (tailstr  (substring str startpos (string-length str)))
	 (endpos   (let loop ((count (- (string-length tailstr) 1)))
		     (if (< count 1)
			 0
			 (if (member (string-ref tailstr count) stripchars)
			     (loop (- count 1))
			     count)))))
    (if (or (< endpos 0)
	    (string=? tailstr ""))
	""
	(substring tailstr 0 (+ endpos 1)))))