1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Strings</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-13.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-15.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc14"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s14">14. Strings</A></H2>
<P>A string is a sequence of characters between double quotes. LISP
represents a string as a variable-length array of characters. You can
write a string which contains a double quote by preceding the quote
with a backslash; a double backslash stands for a single backslash. For
example:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
"abcd" has 4 characters
"\"" has 1 character
"\\" has 1 character
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Here are some functions for dealing with strings:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (concatenate 'string "abcd" "efg")
"abcdefg"
> (char "abc" 1)
#\b ;LISP writes characters preceded by #\
> (aref "abc" 1)
#\b ;remember, strings are really arrays
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The <CODE>concatenate</CODE> function can actually work with any type of sequence:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (concatenate 'string '(#\a #\b) '(#\c))
"abc"
> (concatenate 'list "abc" "de")
(#\a #\b #\c #\d #\e)
> (concatenate 'vector '#(3 3 3) '#(3 3 3))
#(3 3 3 3 3 3)
</PRE>
</CODE></BLOCKQUOTE>
</P>
<HR>
<A HREF="LISP-tutorial-13.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-15.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc14"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|