Skip to main content
Participant
February 21, 2012
Question

Comparison of strings

  • February 21, 2012
  • 3 replies
  • 3746 views

Hello,

I have a problem with the comparison of strings:

If I compare the values 0,5 and 5,0 I will get the result that the values are equal (Case 1).

If I compare the values 0.5 and 5.0 the comparison will be correct (Case 2).

If I compare the values with the method 'compare()' the comparison is correct (Case 3).

Why is the result of case 1 wrong? Is there an explanation or is this a bug?

<cfscript>

    // wrong comparsion

    writeoutput("Case 1: comparsion with 'if' and '==' <br>");

    string_one = "0,5";

    string_two = "5,0";

    writeoutput("values: <font color=green>String one: #String_one#</font> - <font color=red> String Two: #string_two#</font><br><br>");

    if (string_one == string_two) {

        writeoutput("wrong result after the comparsion: #string_one# is not #string_two#<br><br>");

    }

    else {

        writeoutput("right result after the comparsion: #string_one# is not #string_two#<br><br>");

    }

   

    writeoutput("===============================<br>");

   

    // right comparsion

    writeoutput("Case 2: comparsion with 'if' and '==' <br>");

    string_three = "0.5";

    string_four = "5.0";

    writeoutput("values: <font color=green>String three: #String_three#</font> - <font color=red> String four: #string_four#</font><br><br>");

    if (string_three == string_four) {

        writeoutput("wrong result after the comparsion: #string_three# is not #string_four#<br><br>");

    }

    else {

        writeoutput("right result after the comparsion: #string_three# is not #string_four#<br><br>");

    }   

    writeoutput("===============================<br>");

   

    // right comparsion

    writeoutput("Case 3: comparsion with 'compare' <br>");

    string_five = "0,5";

    string_six = "5,0";

   

    writeoutput("values: <font color=green>String five: #String_five#</font> - <font color=red> String six: #string_six#</font><br><br>");

    if (compare(string_five,string_six) EQ 0){

        writeoutput("wrong result after the comparsion: #string_five# is not #string_six#<br><br>");

    }

    else{

        writeoutput("right result after the comparsion: #string_five# is not #string_six#<br><br>");

    }

    writeoutput("===============================<br>");

   

    writeoutput('</strong>');

</cfscript>

This topic has been closed for replies.

3 replies

Inspiring
March 10, 2012

I prefer strong typed language, like Java.

Inspiring
March 22, 2012

Came across this while trying to find a solution wondering if anybody can point me in the right direction

I have two lists, let's say

list1="a,b,c,d"

list2="b,c,d,a"

Is there a quick and easy way to compare the two lists and see if they have exactly the same elements or differ in some way, I don't need to know how they differ, just that they are indeed different, order is not relevant to me either, just the values in the list.

Using COMPARE does not work as it just seems to look at it as a string rather than a list

Without getting into loops and code, is there a command for this (I am using CF8)

Btw Just to clarify list1 would be a #valuelist()# from a SQL query, the other one would already exists as a hard coded list <CFSET list2="">

Thanks

Mark

Inspiring
March 23, 2012

There is no command.  You have to think it through and write something appropriate for your own situation.    

BKBK
Community Expert
Community Expert
February 21, 2012

Speedtossi,

Referring to the news Adam brought from Adobe, I was also surprised when I first found out that isDate("0a") and isDate("3p") both return Yes. I suppose the moral is, when comparing strings, use compare or compareNoCase. When comparing numerical values, don't surround them with quotes.

Inspiring
February 21, 2012

All of this can be somewhat simplified to this:

<cfif "0,6" eq "6,0">

          EQUALS

<cfelse>

          NOT EQUALS

</cfif>

And the answer is - according to CF - "EQUALS".

My reaction to that is:

WTF?

Not much help, sorry.

--

Adam

Owainnorth
Inspiring
February 21, 2012

I'd imagine it's evaluating it as a list, and the two lists are the same despite ordering.

Inspiring
February 21, 2012

It only "works" with (single) digits.

<cfif "0,A" eq "A,0">

          EQUALS

<cfelse>

          NOT EQUALS

</cfif>

and

<cfif "0,66" eq "66,0">

          EQUALS

<cfelse>

          NOT EQUALS

</cfif>

both output "NOT EQUALS".

--

Adam