string compare from external text file

Hi,

I have a problem with string comparison.

This is fine:

string i = "y";

if ( i == "y")
     DoSomething();

But this doesn’t work:

string i = "y";

// Using StreamReader to read a line from a text file and the line is "n" 

i = readline;

if ( i == "n") DoSomething();

( i == "n") <= returns false!!

But when I Debug.log(i), it shows “n” on the console correctly!

I figured out “n” declared in unity script and “n” read from external text file
had different HashCode.

But I couldn’t figure out how to solve this problem.
Help me, please?

If you are getting correct value in Console, then, should verify that -

  • There is no blank space before and after the value

    Solution : use Trim() function to remove extra spaces,

    if( i.Trim() == “n”)

    {

           DoSomething();
    

    }