Jonathan D
Jonathan D

Reputation: 1364

String Compare Issue

I have two strings that i want to compare.

A is a Silverlight user control with the property Header.

B is a plain System.String.

When i compare like so:

if(A.Header == B)

I’m getting that they are not the same.

If I inspect the values in VS2010 with quick watch the values are the same. If I run GetType on both the objects, I find they are both System.String.

I know that i can just compare them with String.Compare.

I though that doing == on strings would always compare the values. Is there something a bit weird with this Silverlight control I am using? Could anyone explain what I am missing here?

Thanks.

Upvotes: 0

Views: 272

Answers (5)

Yannick Motton
Yannick Motton

Reputation: 35971

Might be leading or trailing spaces, difference in casing, maybe it contains characters that look the same, but have a different character code.

Try the following:

if (string.Compare(A.Header.Trim(), B.Trim(), StringComparison.OrdinalIgnoreCase) == 0)
{
  ..
}

Upvotes: 0

Jonathan D
Jonathan D

Reputation: 1364

I found the answer it looks like the equals has been overrided in the silverlight control i am using.

thanks to john in this thread for giving me the answer

Are string.Equals() and == operator really same?

Upvotes: 1

Simone
Simone

Reputation: 11797

Try this:

char[] arrayA = A.Header.ToCharArray();
char[] arrayB = B.ToCharArray();

and inspect them with VS. It should appear clear where they differ.

Upvotes: 0

detunized
detunized

Reputation: 15289

They might have trailing space or something that looks the same, but has different actual character codes. Like a Cyrillic character е might look like Latin e, but they are not the same. Try to iterate over the characters and see if they all the same.

Upvotes: 0

gor
gor

Reputation: 11658

Do they have same length ? Maybe there is trailing or leading space.

Upvotes: 0

Related Questions