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

Function syntax

Compare(string1, string2)

Parameters

Parameter

Description

string1

A string or a variable that contains one

string2

A string or a variable that contains one

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.</p> 
 
<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" method="post"> 
<p>String 1 
<br><input type = "Text" name = "string1"> 
<p>String 2 
<br><input type = "Text" name = "string2"> 
<p><input type = "Submit" value = "Compare these Strings" name = ""> 
    <input type = "RESET"> 
</form>