Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

closed as off-topic by Byte56 May 25 at 1:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Try to Debug.Log both strings, and see what exactly is different, the equals or == should normally work. – Zee May 24 at 23:17
    
That's what I am doing. They are the same – John Demetriou May 25 at 4:05
    
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 – John Demetriou May 25 at 11:48

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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 – John Demetriou May 25 at 11:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.