marko marinovic marko marinovic - 3 months ago 14
C# Question

C# string equals wont work properly

I get response string "true" from php file...
but ,my function always return false ... here is piece of code

public Boolean authorization(String korisnik, String zaporka) {
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["korisnik"] = korisnik;
values["zaporka"] = zaporka;

var response = client.UploadValues("http://localhost/projectX/autorizacija.php", values);
String responseString = Encoding.Default.GetString(response);
System.Diagnostics.Debug.WriteLine(responseString);
if (responseString.Equals("true"))
{
return true;
}
else
{
return false;
}

}

}

Answer

Try:

if (responseString.Trim().Equals("true", StringComparison.InvariantCultureIgnoreCase))
{
  return true;
}
else
{
  System.Diagnostics.Debug.WriteLine(responseString);
  return false;
}

InvariantCultureIgnoreCase = compares strings in a linguistically relevant manner that ignores case Trim = remove whitespaces

And if false, check output value