2

I am running into what must be a HUGE misunderstanding...

I have an object with a string component ID, I am trying to compare this ID to a string in my code in the following way...

if(object.ID == "8jh0086s)
{
//Execute code
}

However, when debugging, I can see that ID is in fact "8jh0086s" but the code is not being executed. I have also tried the following

if(String.Compare(object.ID,"8jh0086s")==0)
{
//Execute code
}

as well as

if(object.ID.Equals("8jh0086s"))
{
//Execute code
}

And I still get nothing...however I do notice that when I am debugging the '0' in the string object.ID does not have a line through it, like the one in the compare string. But I don't know if that is affecting anything. It is not the letter 'o' or 'O', it's a zero but without a line through it.

Any ideas??

13
  • 1
    Yes, your string are not actually identical, and that's why they are compared as different: string object.ID does not have a line through it, like the one in the compare string. Commented Jan 27, 2014 at 22:29
  • Ok...any thoughts on how this can be remedied? Commented Jan 27, 2014 at 22:29
  • What is the type of the property/variable ID of in the object? Commented Jan 27, 2014 at 22:30
  • 1
    Are you talking about Ø? Commented Jan 27, 2014 at 22:30
  • 1
    Try using str.Select(c => (int)c).ToArray() or Encoding.Default.GetBytes to see what characters are actually present in the string. Commented Jan 27, 2014 at 22:33

3 Answers 3

3

I suspect there's something not easily apparent in one of your strings, like a non-printable character for example.

Trying running both strings through this to look at their actual byte values. Both arrays should contain the same numerical values.

var test1 = System.Text.Encoding.UTF8.GetBytes(object.ID);
var test2 = System.Text.Encoding.UTF8.GetBytes("8jh0086s");

==== Update from first comment ====

A very easy way to do this is to use the immediate window or watch statements to execute those statements and view the results without having to modify your code.

Sign up to request clarification or add additional context in comments.

Comments

1

Your first example should be correct. My guess is there is an un-rendered character present in the Object.ID.

You can inspect this further by debugging, copying both values into an editor like Notepad++ and turning on view all symbols.

4 Comments

string instances can't have different encodings; they're always UTF-16 internally.
Oh ok, I was thinking of when a Unicode BOM is present/not present, i have personally ran into that issue before.
There are certainly differences that can exist in strings that should be equivalent (BOM should never make it into a string being used in memory, but that doesn't mean it never happens), they could be different normalisation forms, or be confusables (Т looks a bit like T for example). I'm guessing on a variant of the last. You've the right general idea though, IMO, you just picked the one factor that it's not likely to be in how you phrased it.
Thanks for your feedback Jon, I've removed that statement from the answer.
0

I suspect you answered your own question. If one string has O and the other has 0, then they will compare differently. I have been in similar situations where strings seem the same but they really aren't. Worst-case, write a loop to compare each individual character one at a time and you might find some subtle difference like that.

Alternatively, if object.ID is not a string, but perhaps something of type "object" then look at this: http://blog.coverity.com/2014/01/13/inconsistent-equality

The example uses int, not string, but it can give you an idea of the complications with == when dealing with different objects. But I suspect this is not your problem since you explicitly called String.Compare. That was the right thing to do, and it tells you that the strings really are different!

1 Comment

object ID is a string, thanks for your suggestion I will try that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.