Search code examples
javascriptstringcomparison

String comparison is returning false


Why is this returning false all the time?

var a = new String("17-0069,,Alex Libengood,Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC,Site inspection,,0.55,/mile,0,miles,,1,17-0069><><Alex Libengood><Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC><Site inspection><0.55><1,Mileage").trim();
var b = new String("17-0069,,Alex Libengood,Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC,Site Inspection,,0.55,/mile,0,miles,,1,17-0069><><Alex Libengood><Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC><Site Inspection><0.55><1,Mileage").trim();
if (a === b){
   return true;
} else {
   return false;
}

It's the same string!

I've seen other questions answered by using the trim() method, but it's not working for me. It's like there are hidden characters somewhere in the strings that I can't see. But when I test the lengths, they are both 255 characters long.


Solution

  • It's the same string!

    No, it's not. Look closely:

    "17-0069,,Alex Libengood,Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC,Site inspection,,0.55,/mile,0,miles,,1,17-0069><><Alex Libengood><Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC><Site inspection><0.55><1,Mileage"
    "17-0069,,Alex Libengood,Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC,Site Inspection,,0.55,/mile,0,miles,,1,17-0069><><Alex Libengood><Travel from - Meadors Office - to - 201 Hildebrand Dr, Bonneau SC><Site Inspection><0.55><1,Mileage"
                                                                                                    ^                                                                                                                                    ^
    

    Those are not hidden characters, they're simply different cases of i/I. Found with

    for (let i=0; i<255; i++) if (a[i] != b[i]) console.log(i, a[i], b[i])