I’m trying to compare two strings in C# as I pull them from Excel using the Interop interface. My code is as follows:
public Boolean isSameEdge(Edge e)
{
Boolean result = false;
String e1 = e.getNode1();
String e2 = e.getNode2();
int s1 = String.Compare(e1, Node1);
int s2 = String.Compare(e2, Node2);
if (s2 == 0 && s1 == 0)
result = true;
return result;
}
I’m debugging the code and I see that e1 and Node1 are the EXACT same string, as they should be. Yet String.Compare returns 1 for s1 and -1 for s2. Help!
It is pretty safe to assume that String.Compare() doesn’t have a bug. These strings probably only look the same. Beware of unprintable characters like (char)0. Diagnose with String.ToCharArray().