- Home /
Comparing text from www.downloadhandler.text to string does not work
I have created a script that gets data from a php script. I want to check if I got back "0 results" so I have this code:
Debug.Log(www.downloadHandler.text);
if(www.downloadHandler.text == "0 results") {
Debug.Log("it works");
}
In the Unity adder it first logs "0 results" but it never logs "it works". From the first log I can tell www.downloadHandler.text and "0 results" are the same string but the if statement is false...
Is downloadhandler.text a different format than string or what am I missing?
Please help, thank you
Answer by JDelekto · May 03, 2019 at 11:20 PM
What you might want to do is assign the results of your www.downloadHandler.text to a string variable before the comparison. I also usually prefer to use the .Equals() method to compare strings as well.
For example:
string result = www.downloadHandler.text;
if (result.Equals("0 results")) { ... }
I'm not quite sure what may be happening in your case, but I'm wondering if you're receiving a UTF-8 string while strings in .NET are in Unicode. There is probably a conversion on the download handler's text when logging.
Also, you'll need to be aware of any whitespace you might be missing, after assigning the download handler's text to a string, dump out the length and compare that to the length of the string to which you are doing a compare. If there is any trailing whitespace, for example, it might not be evident. If there is whitespace, trimming the received string may help you there before doing the comparison.
[Edit: balancing parentheticals]
Thank you for the reply. It was in fact a problem with trailing whitespaces! It is weird because I am echoing "0 results" from the php script so why another whitespace is inserted, I don't know... But now I know what was wrong :)
Did you find out exactly what whitespace was trailing? Was it an actual space character (ASCII 32) or was it perhaps a transmitted carriage return, line feed (newline) or carriage return/line feed pair (ASCII 13, and 10 respectively)? If you used a construct (I'm not well versed in PHP) which did something like a "print line" ins$$anonymous$$d of a "print" and flushed the buffer to send to your client, the "print line" may add one (or a combination) of those two characters which one won't see visibly.
I think it was an actual whitespace. I did not check the ASCII of it but I added some text after that and it was just seperated by a space.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to stop WWW from changing request url? 1 Answer
Simple networking - Sending string over network? 1 Answer
Sync player name (Networking) 1 Answer
Double If condition syntax ? 1 Answer
More like a bulletin than chat box 0 Answers