Characters and strings

<-Previous | ^UP^ | Next->

Character and String definitions

Characters are denoted with a # prefix, whilst Strings are represented textually as a series of characters between double quotes. Because Strings are sequences of Characters, any function which acts upon a sequence can be used on a string. Because a string is represented textually as characters enclosed in double quotes, if you want to include literal double quotes in your string you need to escape them with \ (backslash) characters. If you want to include a literal backslash (\) it needs to be escaped with another backslash

GDL-USER> (setq str "Introduction to GendL")

"Introduction to GendL"

GDL-USER> (reverse str)

"LdneG ot noitcudortnI"

GDL-USER> (length str)


GDL-USER> (setq escaped-str "Introduction to "GendL"")

"Introduction to "GendL""

GDL-USER> (length escaped-str)


Character Comparison

Recall from the Equality tutorial that, because a character is an object with the same symbol, any of the tests EQ, EQL, EQUAL or EQUALP will work with characters. Additionally the function char= may be used, which accepts any number of arguments and will return T if the arguments are all the same character. The case-insensitive version is char-equal

GDL-USER> (setq a #c)


GDL-USER> (setq b #c)


GDL-USER> (setq c #C)


GDL-USER> (and (eq a b) (eql a b) (equal a b) (equalp a b))


GDL-USER> (char= a b)


GDL-USER> (char= a c)


GDL-USER> (char-equal a c)


String Comparison

Recall from the Equality tutorial that equal will return T for 2 identical strings and equalp will return T for 2 strings which are identical ignoring case. string= and string-equal will give the same result as equal and equalp, but additionally provide keyword arguments :start1, :end1, start2, end2 which enable substrings to be tested

GDL-USER> (setq str1 "genworks")


GDL-USER> (setq str2 "Genworks")


GDL-USER> (string= str1 str2)


GDL-USER> (string= str1 str2 :start1 1 :start2 1)


GDL-USER> (string-equal str1 str2)


GDL-USER> (string-equal str1 str2 :start1 2)


String Searching/Ordering

When searching or ordering strings, some extra arguments to the functions may be required, because the default test for equality is EQL which, you may recall, will not return T for two identical strings. The keyword input :test generally has to be specified to be an equality test which works with strings, for example string-equal

GDL-USER> (setq str "GendL")


GDL-USER> (position "l" str)


GDL-USER> (position "l" str :test 'string=))


GDL-USER> (position "l" str :test 'string-equal))


GDL-USER> (setq lis (list "Peter" "Paul" "John" "Craig"))

("Peter" "Paul" "John" "Craig")

GDL-USER> (safe-sort lis 'string<)

("Craig""John" "Paul" "Peter")

String Building

One of the easiest ways to build strings is with the CL format function. We will come onto it in the next tutorial but for now it can be though of as a function which defines a format template that you define and then assign specific strings to. At its most basic, the CL function concatenate can achieve the same output

To assemble a string from a list of strings using concatenate the apply function must be used

GDL-USER> (setq str1 "Introduction")


GDL-USER> (setq str2 "To")


GDL-USER> (setq str3 "GendL")


GDL-USER> (format nil "~a ~a ~a" str1 str2 str3)

"Introduction to GendL"

GDL-USER> (concatenate 'string str1 " " str2 " " str3)

"Introduction to GendL"

GDL-USER> (setq lis (list "My" "List" "of" "strings"))

("My" "List" "of" "strings")

GDL-USER> (apply #'concatenate 'string lis)


String Destructuring

Strings may be taken apart using the subseq function.

GDL-USER> (setq str1 "Introduction")


GDL-USER> (subseq str1 3)