Книга: Practical Common Lisp



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.
"foobar" foobar The first backslash escapes second backslash.
""foobar"" "foobar" The backslashes escape quotes.
"foobar" 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"
, on the other hand, will show you the actual string contents:[118]

CL-USER> (format t "foo"bar")

Оглавление книги

Генерация: 0.829. Запросов К БД/Cache: 2 / 0
Вверх Вниз