看起来相同的字符串不相等 [英] Strings that look the same aren't equal

查看:60
本文介绍了看起来相同的字符串不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些单元测试失败.调试后,我在这里找到了问题

Some unit tests are failing. Upon debugging, I traced the problem here

var a = "USD 1,234.12"
var b = "USD 1,234.12"
console.log(a === b)

String a 是由货币格式化程序库生成的,而String b 是由单元测试开发人员编写的.

String a has been generated by a currency formatter library and String b has been written by a unit test developer.

我不明白为什么 === 不能将这两个看起来相同的字符串视为相等.这是怎么回事?

I don't understand why these two strings that look the same aren't considered equal by ===. What is happening here?

推荐答案

您的两个字符串有一个隐藏的区别.运行此:

There's a hidden difference in your two strings. Run this:

var a = "USD 1,234.12"
var b = "USD 1,234.12"

for (var i = 0; i < a.length; i++) {
  console.log(a.codePointAt(i), b.codePointAt(i));
}

b 字符串中的空格是常规空格(32),而 a 字符串中的空格是Unicode不间断空格(160).

The space in the b string is a regular space (32), while the space in the a string is a Unicode non-breaking space (160).

这篇关于看起来相同的字符串不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆