-3

I'm trying to compare to strings in a function to be able to do stuff if it's true. However, for some reason the two strings I'm comparing are never equal to eachother.

I am creating a new string in a function, getting data from a Json and then comparing that string with another string in another class. This doesn't work for some reason. The Debug shows that both strings are equal, but the if-statement returns false.

I created two other string variables (hello & hello2) with the same value ("Hello") and this time it's comparing correctly.

Check the images below. What am I doing wrong? As you can see in the console, both strings have the same value:

Image 1. Here's where I create the string (zoneId).

enter image description here

Image 2. Further down in the same function, same for-loop. Here's where I'm trying to compare the string created in this function with another string from another class. enter image description here

Image 3. As you can see in the console it's looping through the jsonArray. But it's returning false even though it's clear that both strings have the same value. enter image description here

Image 4 and 5. Here I am testing with two other strings inside the function and they are working fine. Does it has something to do calling a string from another class?

enter image description here enter image description here

Here's how my other string in userInfo is set up:

public string userID { get; private set; } 
AStopher
  • 4,207
  • 11
  • 50
  • 75
  • Btw. Here's how my other string in userInfo is set up: public string userID { get; private set; } Does it has something to do with get/set? I'm only getting the value so it shouldn't be any problem? – Alexander Renström Jan 20 '20 at 23:39
  • 1
    It appears `Main.Instance.userInfo.zoneID` has a space in front of the `2`, but `zoneId` does not. Please could you paste the Unity log output into your question instead of posting a screenshot (so I can verify this)? – AStopher Jan 20 '20 at 23:42
  • **[Please don't post code as images!](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question)** – derHugo Jan 21 '20 at 07:10
  • 1
    In general: If the value for this ID is an `int`, why using it as `string` at all? Rather serialize and deserialize it as `int` directly! – derHugo Jan 21 '20 at 07:15

2 Answers2

-1

In Image 3, is there an additional space before the second 2?

How are you processing Main.Instance.userInfo.zoneID?

Freakwave
  • 144
  • 1
  • 7
  • You may be right. There seems to be a space somewhere but I can't figure out how and where. The zoneID from userInfo is coming from www.downloadHandler.text, from a php script. The script is returning the zoneID with echo $row["zoneID"]; – Alexander Renström Jan 21 '20 at 07:58
-1

You need to use string.Equals for string comparison instead of operator == or !=.

Are string.Equals() and == operator really same?