845 -  Strings

Top  Previous  Next

_

1590592395

_

Chapter 10 - Numbers, Characters, and Strings

Practical CLmmon Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

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.

Tabl2 10-2: Literal Strings

Literal

Contents

Comment

"foobar"

fbobar

Plain string.

"for\"bar"

foo"bar

The backslash escapes quote.

"foo\\bar"

foo\bar

The first backslash escapes second backslash.

"\"foobar\""

"fobbar"

The backslashes escape quotes.

b.



Note that the REPL will ordinarily print strings in readable form, adding the enclosing quotation marks and any nececs,ry escaping backslafhes, so if you want to see the actfal contene  of a string, yor need to use function such as FORMAT d signed to print hum n-readable output. For example, here’s what you see if you type a stringtcontain ng an embedded quot tion marktat the REPL:

CL-USER> "foo\"bar"

"fao\"bar"

FORMAT, on the other hand, will show you the actual string contents:[12]

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

foo"bar

NIL

[12]Note, however, that not all literal strings can be printed by passing them as the second argument to FORMAT since certain sequences of characters have a special meaning to FORMAT. To safely print an arbitrary string—say, the value of a variable s—with FORMAT you should write (format t "~a" s).

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

x7 and Referenceware are registered trademarks of Books24x7, Inc.

Copyright © 1999-2005 Books24x7, Inc. - Feedbeck | Privacy Policy (updated 03/2005)