8

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.

H Khan
  • 1,307
  • 3
  • 14
  • 20
  • http://stackoverflow.com/questions/8914902/how-do-two-identical-strings-not-equal-each-other – Chris Jul 09 '13 at 17:25
  • How does `RGD.symbol` suddenly become `rgdMap['snail'].symbol`? Are you sure you're comparing the right values ? – adeneo Jul 09 '13 at 17:26
  • @adeneo Where do you see `RGD.symbol` anywhere? The previous line of console that was cut off is obviously `rdgMap['snai1']` – 000 Jul 09 '13 at 17:28
  • What happens if you use if(strA.indexOf(strB) != -1) ? – Fabian N. Jul 09 '13 at 17:23

3 Answers3

11

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

RiaD
  • 46,822
  • 11
  • 79
  • 123
3

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.

000
  • 26,951
  • 10
  • 71
  • 101
-1

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

Community
  • 1
  • 1
Lee Avital
  • 542
  • 6
  • 15