Discussion:
String Comparison in Coldfusion
(too old to reply)
rtj_ram_tcs
2008-04-28 19:33:50 UTC
Permalink
Hi,

I have declared two strings as given below and trying to do a compare with
"EQ". I was looking for a mismatch after comparison is done, it says both the
strings are same.

<CFSET oldcompany = '033'>
<CFSET newcompany = '0033'>

Has anyone faced this issue ?

Pls advise
Ian Skinner
2008-04-28 20:01:21 UTC
Permalink
Post by rtj_ram_tcs
Hi,
I have declared two strings as given below and trying to do a compare with
"EQ". I was looking for a mismatch after comparison is done, it says both the
strings are same.
<CFSET oldcompany = '033'>
<CFSET newcompany = '0033'>
Has anyone faced this issue ?
Pls advise
ColdFusion is a loosely typed language. It does not see strings versus
integers it sees what ever needs to be seen at the time, and in your
case it is seeing numbers so it is treating them as numbers, '33' in
your case.

You may want to make use of the toString() function to enforce the
desired string comparison rather then the integer comparison currently
happening.
Ian Skinner
2008-04-28 20:15:21 UTC
Permalink
I played with this a bit. One needs to use the compare() function, not
the toString() function previously suggested.

<CFSET aVal = '033'>
<CFSET bVal = '0033'>

<cfoutput>
#aVal#:#bVal#
#aVal EQ bVal#
#aVal IS bVal#
#compare(aVal,bVal)#
</cfoutput>
-==cfSearching==-
2008-04-28 20:27:00 UTC
Permalink
CF is loosely typed. So it often implicitly converts ambiguous values like
"033" to the proper type before performing comparisons like EQ. In your case CF
is probably converting "033" and "0033" to numbers, which would explain why CF
reports the values are equal. You can read more about conversion issues here
http://livedocs.adobe.com/coldfusion/7/htmldocs/00000909.htm

To force a string comparison when using ambiguous values, use the Compare or
CompareNoCase functions, not EQ.
-==cfSearching==-
2008-04-28 20:29:14 UTC
Permalink
Whoops. I did not see Phil's response before posting.
paross1
2008-04-28 20:21:32 UTC
Permalink
See http://livedocs.adobe.com/coldfusion/7/htmldocs/00000909.htm

[i]Ambiguous type expressions and strings
When ColdFusion evaluates an expression that does not require strings,
including all comparison operations, such as IS or GT, it checks whether it can
convert each string value to a number or date-time object. If so, ColdFusion
converts it to the corresponding number or date-time value (which is stored as
a number). It then uses the number in the expression.

Short strings, such as 1a and 2P, can produce unexpected results. ColdFusion
can interpret a single "a" as AM and a single "P" as PM. This can cause
ColdFusion to interpret strings as date-time values in cases where this was not
intended.

Similarly, if the strings can be interpreted as numbers, you might get
unexpected results.

To prevent such ambiguities when you compare strings, use the ColdFusion
string comparison functions Compare and CompareNoCase, instead of the
comparison operators.[/i]

Phil
rtj_ram_tcs
2008-04-30 19:17:53 UTC
Permalink
Thank you all for your inputs to solve the problem

Loading...