【问题标题】:Change to .length with toUpperCase?使用 toUpperCase 更改为 .length?
【发布时间】:2018-09-28 11:08:26
【问题描述】:

我在一个 jQuery 插件中发现了这一行:

node.data.substr(0, pos).toUpperCase().length - node.data.substr(0, pos).length

据我所知,这应该始终为零,因为唯一的区别是toUpperCase(),它不应该改变字符串的长度。

这里发生了什么?

【问题讨论】:

  • 您能否提供一个指向 jQuery 插件源的链接并为我们确定线路?也许它是在它之前的东西。
  • 你想说两次通话的长度不一样?这条线对我来说看起来完全没问题......也许其他的东西会改变字符串......更多的代码会很好。
  • 来自w3schools.com 的注释可能会有所帮助。 toUpperCase() 方法不会改变原始字符串。
  • 第 22 行 - 基本上,他们通过使用 indexOf 来检查匹配项,然后寻找更高或等于 0 的返回值。在此之前,有问题的行会......什么?

标签: javascript string substring string-length


【解决方案1】:

信不信由你,有些字符转换成大写后会变成多个字符。

例如,有一组看起来像两个字母的特殊字符。其中之一是Latin Small Ligature FI,它的存在是因为一种称为“连字”的印刷术。 “FF”、“FL”、“FFI”和“FFL”也有特殊字符。这些将变成来自.toUppercase() 的 2 或 3 个字符。

另一个例子是德语字母eszett。虽然an uppercase version exists 并于 2017 年获得德语正字法委员会的正式批准,但所有浏览器仍将单个字母“ß”转换为两个字母“SS”。

测试程序:

function countUpperDiff(input) {
  console.log((input.toUpperCase().length - input.length) + ": " + input + " → " + input.toUpperCase());
}

console.log("This program prints how many characters longer the uppercase version becomes, followed by a before-and-after view.");

console.log("Some typographic ligatures exist in lowercase, but not uppercase.")
countUpperDiff("fix");
countUpperDiff("fix");
countUpperDiff("fly");
countUpperDiff("fly");
countUpperDiff("off");
countUpperDiff("off");
countUpperDiff("affix");
countUpperDiff("affix");
countUpperDiff("raffle");
countUpperDiff("raffle");

console.log("\nThe German letter ß is NOT converted into the capital letter ẞ, but into SS. This all-caps conversion is a problem for some words:")
countUpperDiff("in massen");
countUpperDiff("in maßen");
console.log("The first one means \"in massive amounts\", while the second one means \"in moderate amounts\".")

输出:

This program prints how many characters longer the uppercase version becomes, followed by a before-and-after view.
Some typographic ligatures exist in lowercase, but not uppercase.
0: fix → FIX
1: fix → FIX
0: fly → FLY
1: fly → FLY
0: off → OFF
1: off → OFF
0: affix → AFFIX
2: affix → AFFIX
0: raffle → RAFFLE
2: raffle → RAFFLE

The German letter ß is NOT converted into the capital letter ẞ, but into SS. This is a problem for some words:
0: in massen → IN MASSEN
1: in maßen → IN MASSEN
The first one means "in massive amounts", while the second one means "in moderate amounts".

【讨论】:

    【解决方案2】:

    一些 Unicode 字符,尤其是连字,在转换为大写时会出现问题,因为它们没有对应的字符,而是可能被转换为 2 个字符。

    代码示例:

    var lowerChar = '\uFB00';
    console.log("lowercase: ", lowerChar, "length: ", lowerChar.length);
    var upperChar = lowerChar.toUpperCase();
    console.log("uppercase: ", upperChar, "length: ", upperChar.length);

    【讨论】:

    • 所以你认为这是对特殊字符的检查?在这种情况下,它并不能很好地完成它 - 如果差异大于匹配的索引,它只会改变程序的行为 - 这显然很脆弱且难以调试。
    • 我不确定这是答案还是意图:/
    • @Andreas 我在一个代码示例中编辑过,你自己看看
    • 我明白了——但以上述方式检查似乎只会引入错误。你能想象一个实际有帮助的案例吗?我想不出一个用例
    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多