[Gardeners] Potting Soil -- strings

Larry Clapp larry at theclapp.org
Thu Apr 27 22:16:58 CDT 2006


The subseq function lets you read from and write to specific portions
of a string (or any sequence, by I'm focusing on strings here):

  (defparameter string (copy-seq "abcdefghijklmnopqrstuvwxyz"))
  (subseq string 12)
  => "mnopqrstuvwxyz"
  (subseq string 12 14)
  => "mn"

  (setf (subseq string 24) "qwer")
  string
  => "abcdefghijklmnopqrstuvwxqw"	; doesn't extend string
  (setf (subseq string 22) "asdf")
  => "abcdefghijklmnopqrstuvasdf"

You can use mapcar to slice a string up by character:

  (setf string "abcdefghijklmnopqrstuvwxyz")
  (mapcar (lambda (i) (char string i))
	  '(3 4 5 12 0 2 17 14))
  => (#\d #\e #\f #\m #\a #\c #\r #\o)
  (mapcar (lambda (i) (subseq string i (1+ i)))
	  '(3 4 5 12 0 2 17 14))
  => ("d" "e" "f" "m" "a" "c" "r" "o")

or by ofsets and lengths (this is vaguely similar to Perl's "unpack"
function):

  (mapcar (lambda (i) (subseq string (first i) (+ (first i) (second i))))
	  '((4 2) (5 3) (6 4)))
  => ("ef" "fgh" "ghij")
  (apply #'concatenate 'string
	 (mapcar (lambda (i) (subseq string (first i) (+ (first i) (second i))))
		 '((4 2) (5 3) (6 4))))
  => "effghghij"

Exchange the first and last characters in a string:

  (defparameter string (copy-seq "make a hat"))
  (rotatef (char string 0) (char string (1- (length string))))
  string
  => "take a ham"

Just a few thoughts before bedtime.  :)  (Inspired by / adapted from
the Perl Cookbook, 1.1, "Accessing Substrings".)

-- Larry




More information about the Gardeners mailing list