Jump to content
  • Advertisement
Sign in to follow this  
Nabren

C# String Compare problem

This topic is 2697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Did you try sticking the result of the ToLower() call into a named string variable and examining that value in the debugger?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Posted · Hidden
Hidden
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!