字符串比较返回false

时间:2019-06-27 21:18:40

标签: javascript string comparison

为什么总是一直返回false?

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;
}

是相同的字符串!

我已经看到其他通过使用trim()方法回答的问题,但是它对我不起作用。就像我看不到的字符串中的某些隐藏字符一样。但是当我测试长度时,它们都为255个字符。

1 个答案:

答案 0 :(得分:2)

  

是相同的字符串!

不,不是。仔细观察:

"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"
                                                                                                ^                                                                                                                                    ^

那些不是隐藏字符,它们只是i / I的不同情况。找到

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