- Home /
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?
Comment
I formatted your question for you. Please learn how to do that for yourself.
Best Answer
Answer by DeveshPandey · Sep 26, 2014 at 11:23 AM
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();
}