H Khan
H Khan

Reputation: 1347

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.

Upvotes: 8

Views: 2795

Answers (3)

RiaD
RiaD

Reputation: 47658

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

Upvotes: 11

Lee Avital
Lee Avital

Reputation: 542

It's likely that you're comparing a string value type to a string.

See Difference between the javascript String Type and String Object?

Solution: either use string.valueOf() or use double equals instead of triple and let javascript coerce the String object to a string value.

From my console a = new String("asdf"); b = "asdf"; a === b; // false a == b; // true a.valueOf() === b.valueOf(); // true

Upvotes: -1

000
000

Reputation: 27247

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.

Upvotes: 3

Related Questions