User Guide

Compare 413
Compare
Description
Performs a case-sensitive comparison of two strings.
Returns
-1, if string1 is less than string2
0, if string1 is equal to string2
1, if string1 is greater than string2
Category
String functions
Function syntax
Compare(string1, string2)
See also
CompareNoCase, Find
Parameters
Usage
Compares the values of corresponding characters in string1 and string2.
Example
<h3>Compare Example</h3>
<p>The compare function performs a <I>case-sensitive</I> comparison of two
strings.
<cfif IsDefined("FORM.string1")>
<cfset comparison = Compare(FORM.string1, FORM.string2)>
<!--- switch on the variable to give various responses --->
<cfswitch expression = #comparison#>
<cfcase value = "-1">
<h3>String 1 is less than String 2</h3>
<I>The strings are not equal</I>
</cfcase>
<cfcase value = "0">
<h3>String 1 is equal to String 2</h3>
<I>The strings are equal!</I>
</cfcase>
<cfcase value = "1">
<h3>String 1 is greater than String 2</h3>
<I>The strings are not equal</I>
</cfcase>
<CFDEFAULTCASE>
<h3>This is the default case</h3>
</CFDEFAULTCASE>
</cfswitch>
</cfif>
<form action = "compare.cfm">
Parameter Description
string1 A string or a variable that contains one
string2 A string or a variable that contains one