C# String Compare problem

Started by
7 comments, last by benryves 12 years, 11 months ago
So I am basically banging my head against a wall at this point. I have a switch on a string.ToLower() with several cases. The weird part is some of the cases hit and some of the cases don't despite me swearing up down left and right that the constants within the case are 100% correct.

So I added a default to try and figure out what's going on and that is failing as well.

default:
{
if (currentValue[0].ToLower() == "id")
{
MessageBox.Show("Equal");
}
else if (currentValue[0].ToLower().Equals("id"))
{
MessageBox.Show("Equal 2");
}

break;
}



currentValue[0] is indeed "ID" when I debug at runtime. So basically it is telling me "id" != "id" and "id".Equals("id") is also false.

What am I missing here? Anyone have any ideas?
Advertisement
Did you try sticking the result of the ToLower() call into a named string variable and examining that value in the debugger?
Just did, same problem.

Here is an image snap of the code and a watch of the values within them.

StringWeirdness.jpg

As you can see currentValue[0] == "ID" definitely returns false, despite that being exactly what is within the value. currentValue[1] == "10000" is still returning true as expected, though...
Is currentValue a string or an array like List<string>? If it's just a string then currentValue[0] will return the first character only.

Do what SiCrane suggested or even Console.WriteLine(String.Format("Current Value: {0}", currentValue[0])); to output the value to the output window.
currentValue is a string[] - sorry, should have clarified.
It's possible you have some non-printing characters in that string. Try examining the length of the string, and each individual character in it.
Try this once:

bool match = (string.Compare(currentValue[0], "id", true) == 0);
Check the length of the string. It's possible that you have some non-printable characters in there.

EDIT: Ninja-ed by Adam.
Mike Popoloski | Journal | SlimDX

It's possible you have some non-printing characters in that string. Try examining the length of the string, and each individual character in it.


Wow, I feel really dumb because this was it. Not sure why I didn't think to check this but in over 5 years of programming this has never happened to me. Haha. Thanks man.
Where is that string coming from? If it's from a file, be warned that on a Turkish version of Windows "ID".ToLower() is ?d, not id; use ToLowerInvariant() instead.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement