Comparing Strings not Working

I’m just comparing two strings, searching for an object, and can’t get a match to read ‘true.’ I’m pretty sure I’m not making a spelling mistake. Here’s the code snippet:

Debug.Log("The name of this game object is:\""+gameObject.name+"\"");
Debug.Log("The name of the parent path is:\""+detailInfo.parentPath+"\"");

if(gameObject.name == detailInfo.parentPath) print ("THEY ARE THE SAME!");
else print("They do not match.");
			
string goName = gameObject.name;
string parentName = detailInfo.parentPath;
Debug.Log("The name of this game object is:\""+goName+"\"");
Debug.Log("The name of the parent path is:\""+parentName+"\"");		

if(goName == parentName) print ("THEY ARE THE SAME!");
else print("They do not match.");

And here is what we see in the Debug window (click on it to enlarge, sorry):

428617--14856--$temp.jpg

As you can see, the letters in the string are the same, they should match. I don’t know what’s going wrong.

Any Ideas???

Thank you !

1 Like

Is it trying to compare the object “gameObject.name” to the object “detailInfo.parentPath”? That’s the only explanation I can think of.

Try using

if ( gameObject.name.Equals( detailInfo.parentPath ) )

maybe that will help.

Try printing the length of both strings; maybe non-printing characters?

–Eric

5 Likes

Vicenti: using Equals they do not match.
Eric: gameObject length is 12 and parentPath is 13. Nice catch.

Working on it.

Maybe it’s a newline character in there. This returns true:

if(detailInfo.parentPath.Contains (gameObject.name))

Thanks, guys.

1 Like

You can use

for (chr in myStringVariableHere) Debug.Log (System.Convert.ToInt32(chr));

to see what the Unicode value is for each character.

–Eric

Eric,
The offending character is code # 13, which is a line return.
To get the string I had read a .txt file from resources and split it something like this:

TextAsset myText = Resources.Load(filePath, typeof(TextAsset)) as TextAsset;
string text = myText.text;
string[] line = text.Split( '\n' );
     // little did I know that in addition to the newline there was a \r in there!!!
     // had to add this line to get rid of the line return:
loadInfo.parentPath = line[0].Replace("\r","");

The split operation took away the newline, I had no idea there was also a line return in there as well.

I couldn’t use:

string[] line = text.Split("\n\r");

because it doesn’t like two characters in there. However, I found another expression that worked properly, with another using statement:

using System.Text.RegularExpressions;

string[] line = Regex.Split(text, "\r\n");

That took away both characters at once, solving the problem once and for all time, forever, done!

Thanks for your help!

1 Like