Compare

Description

Performs a case-sensitive comparison of two strings. Returns:

Category

String functions

Syntax


Compare(string1, string2) 

See also

CompareNoCase, Find

Parameters

Parameter
Description
string1, string2
Strings to compare

Usage

The comparison is performed on the ASCII values (character codes) of corresponding characters in string1 and string2.

If many strings are sorted in increasing order based on the Compare function, they appear listed in dictionary order.

Example

<!--- This example shows the use of Compare --->

<html>

<head>

<title>

Compare Example

</title>

</head>



<body bgcolor = silver>

<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" 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>



</body>

</html>    



Banner.Novgorod.Ru