emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node166.html

157 lines
8.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>18.2. String Comparison</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" String Comparison">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html3598 HREF="node167.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3596 HREF="node164.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3590 HREF="node165.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3600 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3601 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3599 HREF="node167.html"> String Construction and </A>
<B>Up:</B> <A NAME=tex2html3597 HREF="node164.html"> Strings</A>
<B> Previous:</B> <A NAME=tex2html3591 HREF="node165.html"> String Access</A>
<HR> <P>
<H1><A NAME=SECTION002220000000000000000>18.2. String Comparison</A></H1>
<P>
The naming conventions for these functions and for their keyword
arguments generally follow the conventions for the generic sequence
functions (see chapter <A HREF="node141.html#KSEQUE">14</A>).
<p>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
Note that this remark, predating the design of the Common Lisp Object System,
uses the term ``generic'' in a generic sense and not necessarily
in the technical sense used by CLOS
(see chapter <A HREF="node15.html#DTYPES">2</A>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<tt>string= <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2</tt><P><tt>string=</tt> compares two strings and is true if
they are the same (corresponding characters are identical)
but is false if they are not.
The function <tt>equal</tt> calls <tt>string=</tt> if
applied to two strings.
<P>
The keyword arguments <tt>:start1</tt> and <tt>:start2</tt> are the places
in the strings to start the comparison.
The arguments <tt>:end1</tt> and <tt>:end2</tt> are the
places in the strings to stop comparing; comparison stops just
<i>before</i> the position specified by a limit.
The ``start'' arguments default to zero (beginning of string),
and the ``end'' arguments (if either omitted or <tt>nil</tt>)
default to the lengths of the strings (end of string),
so that by default the entirety of each string is examined.
These arguments are provided so that substrings can be compared
efficiently.
<P>
<tt>string=</tt> is necessarily false if the (sub)strings
being compared are of unequal length; that is, if
<P><pre>
(not (= (- end1 start1) (- end2 start2)))
</pre><P>
is true, then <tt>string=</tt> is false.
<P>
<P><pre>
(string= &quot;foo&quot; &quot;foo&quot;) is true
(string= &quot;foo&quot; &quot;Foo&quot;) is false
(string= &quot;foo&quot; &quot;bar&quot;) is false
(string= &quot;together&quot; &quot;frog&quot; :start1 1 :end1 3 :start2 2)
is true
</pre><P>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1989 (STRING-COERCION) <A NAME=18203>&#160;</A>
to clarify string coercion (see <tt>string</tt>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<hr>
<b>Compatibility note:</b> <tt>string=</tt> is called <tt>strequal</tt> in Interlisp.
<hr>
<P>
<BR><b>[Function]</b><BR>
<tt>string-equal <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2</tt><P><tt>string-equal</tt> is just like <tt>string=</tt> except that differences
in case are ignored; two characters are considered to be the same
if <tt>char-equal</tt> is true of them.
For example:
<P><pre>
(string-equal &quot;foo&quot; &quot;Foo&quot;) is true
</pre><P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1989 (STRING-COERCION) <A NAME=18219>&#160;</A>
to clarify string coercion (see <tt>string</tt>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<pre>
string&lt; <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string&gt; <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string&lt;= <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string&gt;= <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string/= <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
</pre>
<P>These functions compare the two string arguments lexicographically,
and the result is <tt>nil</tt> unless <i>string1</i> is respectively
less than, greater than,
less than or equal to, greater than or equal to, or not equal to <i>string2</i>.
If the condition is satisfied, however, then
the result is the index within the strings of the first character
position at which the strings fail to match; put another way,
the result is the length of the longest common prefix of the strings.
<P>
A string <i>a</i> is less than a string <i>b</i> if
in the first position in which they differ the character of <i>a</i>
is less than the corresponding character of <i>b</i> according to
the function <tt>char&lt;</tt>, or
if string <i>a</i> is a proper prefix of string <i>b</i>
(of shorter length and matching in all the characters of <i>a</i>).
<P>
The keyword arguments <tt>:start1</tt> and <tt>:start2</tt> are the places
in the strings to start the comparison.
The keyword arguments <tt>:end1</tt> and <tt>:end2</tt>
are the places in the strings to stop comparing; comparison stops just
<i>before</i> the position specified by a limit.
The ``start'' arguments default to zero (beginning of string),
and the ``end'' arguments (if either omitted or <tt>nil</tt>)
default to the lengths of the strings (end of string),
so that by default the entirety of each string is examined.
These arguments are provided so that substrings can be compared
efficiently. The index returned in case of a mismatch
is an index into <i>string1</i>.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1989 (STRING-COERCION) <A NAME=18243>&#160;</A>
to clarify string coercion (see <tt>string</tt>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Function]</b><BR>
<pre>
string-lessp <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string-greaterp <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string-not-greaterp <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string-not-lessp <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
string-not-equal <i>string1</i> <i>string2</i> &amp;key :start1 :end1 :start2 :end2
</pre>
<P>These are exactly like <tt>string&lt;</tt>, <tt>string&gt;</tt>, <tt>string&lt;=</tt>,
<tt>string&gt;=</tt>, and <tt>string/=</tt>, respectively, except that distinctions between
uppercase and lowercase letters are ignored. It is as if
<tt>char-lessp</tt> were used instead of <tt>char&lt;</tt>
for comparing characters.
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1989 (STRING-COERCION) <A NAME=18256>&#160;</A>
to clarify string coercion (see <tt>string</tt>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html3598 HREF="node167.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3596 HREF="node164.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3590 HREF="node165.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3600 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3601 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3599 HREF="node167.html"> String Construction and </A>
<B>Up:</B> <A NAME=tex2html3597 HREF="node164.html"> Strings</A>
<B> Previous:</B> <A NAME=tex2html3591 HREF="node165.html"> String Access</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>