开发者

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 comp开发者_Go百科are the values. Is there something a bit weird with this Silverlight control I am using? Could anyone explain what I am missing here?

Thanks.


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?


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


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.


Try this:

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

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


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)
{
  ..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜