- Home /
String.Equals returning false?
Hey, I see that someone asked something like that, but I think my problem is different.
So:
I have a TextAsset named teste.
the first line of this .txt file is "title"
And I have this function:
void updateText(){
string[] lines = teste.text.Split('\n');
foreach (string line in lines){
if (line.Equals("title")) {
//Do something
}
Debug.Log(line);
}
}
Even that Debug.Log is returning to me title, but that if statement is always returning false and I don't understand why. Care to help?
I've tried checking both strings lengths and line whas 6 long while "title"whas 5 long. But after adding '\n' to "title", still doesn't work
Answer by sharat · Oct 16, 2015 at 11:41 PM
You can try removing extra whitespace by using the Trim() function.
if (line.Trim().Equals("title")) {
If the length is different, this is almost certainly your issue. An extra space, newline, tab, etc.
Right, if it's a "normal" txt file created on windows each line is seperated by a carriage return (\r) followed by a line feed (\n). Since the text is split on the line feed character the "\r" would be still there at the end of the line.
Thanks a lot, guys! I'll try your suggestions as soon as I can. I'll be back with the results.
It worked! That \r probably was messing things up. Thanks a lot!
Answer by C_Randy · Oct 16, 2015 at 10:48 PM
You could try:
if (string.Compare(line, "title") == 0) //Do things
Your answer
Follow this Question
Related Questions
Problem with StringReader or TextAsset not reading some characters 1 Answer
How can i split a string[] ? 1 Answer
How to extract part of a string variable? 1 Answer
Does Unity's Mono do a reference compare in its string == operator? 2 Answers
Performace Overhit for String Concatenation in Online Pool Game Posted 0 Answers