Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason why a JS string would not equal itself? (see photo)

Is there any reason why a string wouldn't equal itself? I am processing a large amount of strings and some strings are, in the end, not equaling themselves.

Here is an image that shows what I am talking about.

Example Image

Any ideas?

RESOLVED: There is a carriage return (\r) attached to the end of one of the strings. I used String.length to see the lengths of each string, and they differed (5 and 6). I then looked through the string using String.charAt. In Chrome, this showed an empty string (""). However, the empty string was not a falsy value. I tried this in FireFox and it showed the carriage return.

like image 627
H Khan Avatar asked Jul 09 '13 17:07

H Khan


2 Answers

In any language if one string contains some unreadable characters, and the other doesn't they will be different even if they seem to be equal by human.

Try print their lengths and print them char-to-char

like image 77
RiaD Avatar answered Nov 13 '22 01:11

RiaD


It could be that one of the characters is a UTF-8 representation of the ascii equivalent.

Here is a transliteration jquery plugin: http://code.google.com/p/jquery-transliteration-plugin/wiki/Usage

There could also be a control sequence at the end, like a BOM.

like image 40
Joe Frambach Avatar answered Nov 13 '22 00:11

Joe Frambach