0
\$\begingroup\$

I am trying to compare two strings in C# but even though I print the strings and they are the same, the check always fails. Only one is stored in a variable and the other is hardcoded. I tried using the ==operator and the equals operator. Also tried with using the IgnoreCase StringComparisonoption but still nothing. I also checked the length of the string in case there are leading or following spaces or something but it's the same. IF it is any help the string is coming from GlovePIE

What could be going wrong?

\$\endgroup\$
3
  • \$\begingroup\$ Try to Debug.Log both strings, and see what exactly is different, the equals or == should normally work. \$\endgroup\$
    – Zee
    May 24 '15 at 23:17
  • \$\begingroup\$ That's what I am doing. They are the same \$\endgroup\$ May 25 '15 at 4:05
  • \$\begingroup\$ As it turns out there was a problem with cultural settings, when printing they looked exactly the same but inside the variable they were not, unprintable characters existed in the string \$\endgroup\$ May 25 '15 at 11:48
2
\$\begingroup\$

If you want to be hardcore about it...

public static bool StringComparison (string s1, string s2)
{
    if (s1.Length != s2.Length) return false;
    for (int i = 0; i < s1.Length; i++)
    {
        if (s1[i] != s2[i]) {
            Debug.Log ("The " + i.ToString() + "th character is different.");
            return false;
        }
    }
    return true;
}

If this returns false, then you are 100% sure that your strings are different. If it returns true, you might have just found a tremendous bug in .NET. This method will also tell you which character is different.

\$\endgroup\$
1
  • \$\begingroup\$ As it turns out there was a problem with cultural settings, when printing they looked exactly the same but inside the variable they were not, unprintable characters existed in the string \$\endgroup\$ May 25 '15 at 11:48

Not the answer you're looking for? Browse other questions tagged .