emacs.d/clones/lisp/www.csci.viu.ca/~wesselsd/courses/csci330/code/lisp/strings.cl.html
2022-10-07 19:32:11 +02:00

54 lines
1.2 KiB
HTML

; string functions
; (strings are simply vectors of characters)
; sequence functions work on strings, e.g.
(length str)
(concatenate 'string "foo" "blah") ; returns "fooblah"
(elt "abcdefg" 2) ; returns #\c
(count "count the cs" #\c) ; returns 2
(substitute #\x #\X "extra") ; returns "eXtra"
; etc
; get the i'th character from a string
(char str 3) ; like elt
; convert between integers and strings
(parse-integer "42")
; using read/write to convert between anything (lisp recognizes) and strings
(read-from-string str)
(write-to-string val)
; using format convert to a number into a string
(setf N 12345)
(setf str (format nil "~A" N))
; convert to upper/lowercase
(string-upcase str)
(string-downcase str)
; convert a symbol to a string, e.g. var to "var"
(symbol-name x)
; convert a string to a symbol
(intern sym)
; compare strings
(setf str1 "hello")
(setf str2 "hi")
(string= str1 str2)
(string/= str1 str2)
(string<= str1 str2)
(string>= str1 str2)
(string< str1 str2)
(string> str1 str2)
(string= str1 str2)
; convert a string to a list of characters
(defun str2list (str)
(if (stringp str) (loop for char across str collect char)))
; convert a list to a string
(defun list2str (L)
(if (listp L) (format nil "~{~A~}" L)))