Home All Groups Group Topic Archive Search About

False Positives From String Comparison using string.Equals()

Author
20 Jun 2009 6:29 AM
Smithers
I am using the .Equals() method of the string class, as follows, to compare
string properties of two different instances of a given class.


if
(!valuesFromUI.Other_Description.Equals(valuesAsRetrievedFromDB.Other_Description,
StringComparison.OrdinalIgnoreCase))
{
   return true;
}

What I am observing in testing is that the above .Equals() periodically
finds that the string values are different when they do not in fact appear
to be different in any way. By "appear to be different in any way" I mean
that I hover the mouse over each property and view in the "Text Visualizer",
then copy from there into Notepad and compare the values in notepad - they
appear to be the exact same string.

The bigger picture is this: I'm using Visual Studio Pro 2008. This is
happening in an ASP.NET Web application. I instantiate the class and
populate it with values from the underlying SQL Server 2005 database, then
load those properties into the UI, and then store the object instance in a
Session variable. Then, when the user navigates to a different page, I
instantiate a new instance of the class and populate it with values from the
UI. I then compare relevant properties of that instance - as in the sample
code above - against the same properties from the instance that was
previously stored in Session state. I do this to determine if the user
actually modified any values from the underlying database and therefore
whether to save any changes to the database.

How can I compare the strings in a way that would not result in such "false
positives"? I would think my approach, above, is perfectly fine. What can I
do differently?

Thanks.

Author
20 Jun 2009 3:34 PM
Michael Covington
Is there any way you could capture and post an example of two strings
that compare different but do not appear different?

Or unpack them into their component bytes and display them in hex?
(Some other C# programmer who has the method at his fingertips will, I
hope, chime in with an exact method of doing that.)
Are all your drivers up to date? click for free checkup

Author
20 Jun 2009 3:55 PM
Michael Covington
Michael Covington wrote:
> Is there any way you could capture and post an example of two strings
> that compare different but do not appear different?
>
> Or unpack them into their component bytes and display them in hex? (Some
> other C# programmer who has the method at his fingertips will, I hope,
> chime in with an exact method of doing that.)


I'll chime in myself.  Do this:


string s = "Hello, world!"; // just an example

foreach (char c in s) Console.WriteLine("{0:X4}", (int)c);


That gives you 4 hex digits (sufficient for Unicode).
Author
21 Jun 2009 7:15 AM
Cor Ligthert[MVP]
This is possible as the DataBase returns spaces.

However, simply testing (StringX == StringY) gives the same result and in my
opinion much easier then let it go trough an extra old derived C function.

Cor


Show quoteHide quote
"Smithers" <A@B.com> wrote in message
news:uN9EGCX8JHA.5040@TK2MSFTNGP04.phx.gbl...
>I am using the .Equals() method of the string class, as follows, to compare
>string properties of two different instances of a given class.
>
>
> if
> (!valuesFromUI.Other_Description.Equals(valuesAsRetrievedFromDB.Other_Description,
> StringComparison.OrdinalIgnoreCase))
> {
>   return true;
> }
>
> What I am observing in testing is that the above .Equals() periodically
> finds that the string values are different when they do not in fact appear
> to be different in any way. By "appear to be different in any way" I mean
> that I hover the mouse over each property and view in the "Text
> Visualizer", then copy from there into Notepad and compare the values in
> notepad - they appear to be the exact same string.
>
> The bigger picture is this: I'm using Visual Studio Pro 2008. This is
> happening in an ASP.NET Web application. I instantiate the class and
> populate it with values from the underlying SQL Server 2005 database, then
> load those properties into the UI, and then store the object instance in a
> Session variable. Then, when the user navigates to a different page, I
> instantiate a new instance of the class and populate it with values from
> the UI. I then compare relevant properties of that instance - as in the
> sample code above - against the same properties from the instance that was
> previously stored in Session state. I do this to determine if the user
> actually modified any values from the underlying database and therefore
> whether to save any changes to the database.
>
> How can I compare the strings in a way that would not result in such
> "false positives"? I would think my approach, above, is perfectly fine.
> What can I do differently?
>
> Thanks.
>
Author
22 Jun 2009 8:06 AM
Hans Kesting
It happens that Cor Ligthert[MVP] formulated :
> This is possible as the DataBase returns spaces.
>
> However, simply testing (StringX == StringY) gives the same result and in my
> opinion much easier then let it go trough an extra old derived C function.
>
> Cor
>

No, that would not give the same results.
"A" == "a"  would return false,
"A".Equals("a", StringComparison.OrdinalIgnoreCase) would return true.

You are right however to point to the possibility of extra spaces in
the "database" value. They would not be visible but would mess up the
comparison.
As a hint to the OP: that's why I don't just "Console.WriteLine" those
strings, but add some sort of brackets around them:
   Console.WriteLine("[" + stringToDisplay + "]");
Then extra spaces would become apparent.

Hans Kesting

Show quoteHide quote
>
> "Smithers" <A@B.com> wrote in message
> news:uN9EGCX8JHA.5040@TK2MSFTNGP04.phx.gbl...
>>I am using the .Equals() method of the string class, as follows, to compare
>> string properties of two different instances of a given class.
>>
>>
>> if
>> (!valuesFromUI.Other_Description.Equals(valuesAsRetrievedFromDB.Other_Description,
>> StringComparison.OrdinalIgnoreCase))
>> {
>>   return true;
>> }
>>
>> What I am observing in testing is that the above .Equals() periodically
>> finds that the string values are different when they do not in fact appear
>> to be different in any way. By "appear to be different in any way" I mean
>> that I hover the mouse over each property and view in the "Text
>> Visualizer", then copy from there into Notepad and compare the values in
>> notepad - they appear to be the exact same string.
>>
>> The bigger picture is this: I'm using Visual Studio Pro 2008. This is
>> happening in an ASP.NET Web application. I instantiate the class and
>> populate it with values from the underlying SQL Server 2005 database, then
>> load those properties into the UI, and then store the object instance in a
>> Session variable. Then, when the user navigates to a different page, I
>> instantiate a new instance of the class and populate it with values from
>> the UI. I then compare relevant properties of that instance - as in the
>> sample code above - against the same properties from the instance that was
>> previously stored in Session state. I do this to determine if the user
>> actually modified any values from the underlying database and therefore
>> whether to save any changes to the database.
>>
>> How can I compare the strings in a way that would not result in such "false
>> positives"? I would think my approach, above, is perfectly fine. What can I
>> do differently?
>>
>> Thanks.
>>
Author
22 Jun 2009 2:13 PM
Merk
<< SNIP>>


> As a hint to the OP: that's why I don't just "Console.WriteLine" those
> strings, but add some sort of brackets around them:
>   Console.WriteLine("[" + stringToDisplay + "]");
> Then extra spaces would become apparent.


Upon reading from the DB and UI, each string is trimmed (theString.Trim()).
So, I think that would resolve any extra spaces issue.

I have yet to try the suggestion by Mr. Covington. That looks like it might
shed some light on the situation.

-S
Author
22 Jun 2009 2:37 PM
Cor Ligthert[MVP]
Hans,

Most people here are clever enough that they know that it is possible to
write, I take mostly the shortest notation when I want to show something.

("A".ToUpper()  ==  "a".ToUpper())

Cor

Show quoteHide quote
"Hans Kesting" <news.hansdk@spamgourmet.com> wrote in message
news:ObtqYBx8JHA.4444@TK2MSFTNGP05.phx.gbl...
> It happens that Cor Ligthert[MVP] formulated :
>> This is possible as the DataBase returns spaces.
>>
>> However, simply testing (StringX == StringY) gives the same result and in
>> my opinion much easier then let it go trough an extra old derived C
>> function.
>>
>> Cor
>>
>
> No, that would not give the same results.
> "A" == "a"  would return false,
> "A".Equals("a", StringComparison.OrdinalIgnoreCase) would return true.
>
> You are right however to point to the possibility of extra spaces in the
> "database" value. They would not be visible but would mess up the
> comparison.
> As a hint to the OP: that's why I don't just "Console.WriteLine" those
> strings, but add some sort of brackets around them:
>   Console.WriteLine("[" + stringToDisplay + "]");
> Then extra spaces would become apparent.
>
> Hans Kesting
>
>>
>> "Smithers" <A@B.com> wrote in message
>> news:uN9EGCX8JHA.5040@TK2MSFTNGP04.phx.gbl...
>>>I am using the .Equals() method of the string class, as follows, to
>>>compare string properties of two different instances of a given class.
>>>
>>>
>>> if
>>> (!valuesFromUI.Other_Description.Equals(valuesAsRetrievedFromDB.Other_Description,
>>> StringComparison.OrdinalIgnoreCase))
>>> {
>>>   return true;
>>> }
>>>
>>> What I am observing in testing is that the above .Equals() periodically
>>> finds that the string values are different when they do not in fact
>>> appear to be different in any way. By "appear to be different in any
>>> way" I mean that I hover the mouse over each property and view in the
>>> "Text Visualizer", then copy from there into Notepad and compare the
>>> values in notepad - they appear to be the exact same string.
>>>
>>> The bigger picture is this: I'm using Visual Studio Pro 2008. This is
>>> happening in an ASP.NET Web application. I instantiate the class and
>>> populate it with values from the underlying SQL Server 2005 database,
>>> then load those properties into the UI, and then store the object
>>> instance in a Session variable. Then, when the user navigates to a
>>> different page, I instantiate a new instance of the class and populate
>>> it with values from the UI. I then compare relevant properties of that
>>> instance - as in the sample code above - against the same properties
>>> from the instance that was previously stored in Session state. I do this
>>> to determine if the user actually modified any values from the
>>> underlying database and therefore whether to save any changes to the
>>> database.
>>>
>>> How can I compare the strings in a way that would not result in such
>>> "false positives"? I would think my approach, above, is perfectly fine.
>>> What can I do differently?
>>>
>>> Thanks.
>>>
>
>
Author
23 Jun 2009 8:01 AM
Göran_Andersson
Cor Ligthert[MVP] wrote:
> Hans,
>
> Most people here are clever enough that they know that it is possible to
> write, I take mostly the shortest notation when I want to show something.
>
> ("A".ToUpper()  ==  "a".ToUpper())
>
> Cor
>

Why would you process the strings just to use one comparison, when there
is another comparison that does exactly what you want without first
processing the strings?

--
Göran Andersson
_____
http://www.guffa.com
Author
24 Jun 2009 12:55 PM
Cor Ligthert[MVP]
Goran,

Most people are clever enough to know what is done behind the scene.

Strings to Upper doesn't process the string, it only takes a subset of the
ascii code, it does not work by instance with the Turkish uppercase i.

Cor


Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:%23WtvSj98JHA.3544@TK2MSFTNGP04.phx.gbl...
> Cor Ligthert[MVP] wrote:
>> Hans,
>>
>> Most people here are clever enough that they know that it is possible to
>> write, I take mostly the shortest notation when I want to show something.
>>
>> ("A".ToUpper()  ==  "a".ToUpper())
>>
>> Cor
>>
>
> Why would you process the strings just to use one comparison, when there
> is another comparison that does exactly what you want without first
> processing the strings?
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
Author
24 Jun 2009 7:53 PM
Merk
RE
> Most people are clever enough to know what is done behind the scene.


No - they are not!

"Most people" can't even, well, I better stop right there...
Author
26 Jun 2009 10:57 PM
Göran_Andersson
Cor Ligthert[MVP] wrote:
> Goran,
>
> Most people are clever enough to know what is done behind the scene.

What do you mean by that?

> Strings to Upper doesn't process the string,

Of course it does. A method that just returns the same string without
doing anything would be pretty useless, would it not?

> it only takes a subset of
> the ascii code, it does not work by instance with the Turkish uppercase i.

How is that relevant?

--
Göran Andersson
_____
http://www.guffa.com
Author
7 Jul 2009 1:23 AM
Ben Voigt [C++ MVP]
Show quote Hide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:uNUy5Fr9JHA.3244@TK2MSFTNGP03.phx.gbl...
> Cor Ligthert[MVP] wrote:
>> Goran,
>>
>> Most people are clever enough to know what is done behind the scene.
>
> What do you mean by that?
>
>> Strings to Upper doesn't process the string,
>
> Of course it does. A method that just returns the same string without
> doing anything would be pretty useless, would it not?
>
>> it only takes a subset of the ascii code, it does not work by instance
>> with the Turkish uppercase i.
>
> How is that relevant?

There exist strings a, b, c, and d for which

a.ToUpper() == b.ToUpper() but string.Equal(a,b,IgnoreCase) is false

c.ToUpper() != d.ToUpper() but string.Equal(c,d,IgnoreCase) is true

Show quoteHide quote
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4221 (20090706) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4221 (20090706) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Bookmark and Share