Strings
As mentioned earlier, strings in Common Lisp are really a composite data type, namely, a one-dimensional array of characters. Consequently, I’ll cover many of the things you can do with strings in the next chapter when I discuss the many functions for manipulating sequences, of which strings are just one type. But strings also have their own literal syntax and a library of functions for performing string-specific operations. I’ll discuss these aspects of strings in this chapter and leave the others for Chapter 11.
As you’ve seen, literal strings are written enclosed in double quotes. You can include any character supported by the character set in a literal string except double quote ("
) and backslash (\). And you can include these two as well if you escape them with a backslash. In fact, backslash always escapes the next character, whatever it is, though this isn’t necessary for any character except for "
and itself. Table 10-2 shows how various literal strings will be read by the Lisp reader.
Table 10-2. Literal Strings
Literal | Contents | Comment |
“foobar” | foobar | Plain string. |
“foo\”bar” | foo”bar | The backslash escapes quote. |
“foo\bar” | foo\bar | The first backslash escapes second backslash. |
“\”foobar\”” | “foobar” | The backslashes escape quotes. |
“foo\bar” | foobar | The backslash “escapes” b |
Note that the REPL will ordinarily print strings in readable form, adding the enclosing quotation marks and any necessary escaping backslashes, so if you want to see the actual contents of a string, you need to use function such as **FORMAT**
designed to print human-readable output. For example, here’s what you see if you type a string containing an embedded quotation mark at the REPL:
CL-USER> "foo\"bar"
"foo\"bar"
**FORMAT**
, on the other hand, will show you the actual string contents:12
CL-USER> (format t "foo\"bar")
foo"bar
NIL