The Simplest Scripting Problem in the History of Scripting

Hi all,
I’m shocked and appalled at myself for having to ask about this, but I’m getting nowhere. Basically, I’m having trouble comparing two strings in C#. Yes, you read that right, it’s something I must have done hundreds of times in my project before, but for some reason, I’m not getting the results I’m expecting.

To keep it short, I’ve got two variables, one of these comes from PlayerPrefs, the other is read from a external file on the hard drive. I’m comparing them, then doing what I need to do depending on the result, easy, right?

So far I’ve tried:

if(str1 == str2){
// do stuff
}
else {
// do other stuff
}

Also

if(string.Equals(str1, str2)){
// do stuff
}
else {
// do other stuff
}

Theres no problem with the code here, theres no errors in it and it works as you would expect- depending on the contents of the string. To explain- when the contents of str1 and str2 are “BARNEY”, the comparison is true, and the expected code executes. However, if the contents are “M DOG”, the comparison fails, every single time. Even when I debug log the contents of the string, they look exactly the same to me, but the comparison fails.

So I put my thinking cap on. A wild stab in the dark, I thought maybe it’s the blank space giving me problems, it was worth a shot. So I used str1.Replace(" ", string.Empty) and did the same for str2. But I get the same results, the space is gone, the strings look equal in the console, but the function comparing them is still returning false every time.

I tried replacing the contents of the variables manually, instead of from the text file and from player prefs, and the comparison works flawlessly. So I guess I’m asking is there something happening that could make two strings appear the same to me, but not to my code, when one is read from a text file and one is from playerprefs?

Things I haven’t tried:

  • Asking my cat
  • Throwing my machine out the window
  • Making a thread comparing Unity to UE

Thanks for any input, solutions or out right mockery (I kinda deserve it)!

Are you sure you don’t have a zero instead of an “O” on one of them? Just a thought.

Another thought: when you did your replacement of a blank space with an empty string, did you look at the results in the debugger? Maybe you have a weird character in there that only looks like a space?

1 Like

Definitely worth a shot. Google rubber ducking. :wink:

Using the == operator should work, assuming you are treating the strings as strings. If you’ve somehow managed to box them into objects then == will behave like ReferenceEquals, giving you odd results.

Make sure your logic is sound by setting string1 = string2 immediately before this code runs.

As steve said, I think its an encoding issue. The blank space in your file is a different encoding than c# strings.

Maybe try some of the things in this thread that talk about the StringComparison enum.
http://stackoverflow.com/questions/12622243/string-equals-method-fails-even-the-two-strings-are-same-in-c

1 Like

If you are hunting down character set differences you could always do a char by char comparison to see which one is failing.

if (string1.length==string2.length){
    for (int i = 0; i < string1.length; i++){
        bool result = (string1[i] == string2[i]);
        Debug.Log(string1[i] + " == " + string2[i] + " :" + result.ToString());
    }
}

Thanks for the input guys! It turns out, that although the two strings look identical, the one from the text file is one character longer, something I didn’t notice until trying out @Kiwasi 's code snippet and it failed to run. I guess I didn’t account for the length of invisible characters when comparing them by eye!

I’ve had a look through that link from @HiddenMonk and tried a couple of bits, but even after removing the space completely, it’s still measuring up one character longer…

Anyway, It’s 2:30 AM, Unity just threw some memory allocation error, so I’m throwing in the towel for tonight. At least I know what I’m doing tomorrow now :face_with_spiral_eyes:

1 Like

You could take out the length check from my script. It should then identify the extra character for you. (It may also throw an index out of range exception. But that’s no big deal).

Yup, I tried that out last night, but was too tired to make sense of the outcome- every letter was matching, and there was no out of range error until it looked like the comparison finished. This morning, I decided to take a fresh look at the code I’d written quite a while ago that generates the text file.

Then it hit me. I’d been using “\r\n” for new lines when writing to the file. The mystery character was a “\r” at the end of the string from the text file. What a ride that was. I’ve never had issues with these invisible characters before, but at least the lesson is learned, even if I had to do it the hard way!

Thanks to everyone for chipping in and @Kiwasi for the code snippet that triggered the cogs in my head!

2 Likes