如何在Javascript中比较Unicode字符串? [英] How to compare Unicode strings in Javascript?

查看:138
本文介绍了如何在Javascript中比较Unicode字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用JavaScript编写Ł> Z它返回 true 。在Unicode顺序中,它当然应该 false 。如何解决这个问题?我的网站使用的是UTF-8。

When I wrote in JavaScript "Ł" > "Z" it returns true. In Unicode order it should be of course false. How to fix this? My site is using UTF-8.

推荐答案

您可以使用 Intl.Collat​​or String.prototype.localeCompare ,由 ECMAScript国际化API

"Ł".localeCompare("Z", "pl");              // -1
new Intl.Collator("pl").compare("Ł","Z");  // -1

-1 Ł Z 之前。

注意,它只适用于最新的浏览器。

Note it only works on latest browsers, though.

这篇关于如何在Javascript中比较Unicode字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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