【问题标题】:How can I convert a string into a unicode character?如何将字符串转换为 unicode 字符?
【发布时间】:2011-10-27 03:43:28
【问题描述】:

在 Javascript 中,'\uXXXX' 返回一个 unicode 字符。但是,当XXXX 部分是变量时,我怎样才能获得 unicode 字符?

例如:

var input = '2122';
console.log('\\u' + input);             // returns a string: "\u2122"
console.log(new String('\\u' + input)); // returns a string: "\u2122"

我能想到的唯一方法是使用eval;但我希望有更好的解决方案:

var input = '2122';
var char = '\\u' + input;
console.log(eval("'" + char + "'"));    // returns a character: "™"

【问题讨论】:

    标签: javascript unicode


    【解决方案1】:

    像这样使用String.fromCharCode()String.fromCharCode(parseInt(input,16))。当您使用\u 将Unicode 值放入字符串时,它会被解释为十六进制值,因此您需要在使用parseInt 时指定基数(16)。

    【讨论】:

    • 感谢您将我链接到 fromCharCode(),但这仍然无法将 2122 转换为商标标志
    • parseInt(input, 16) 似乎可以完成这项工作;)
    • 我猜您希望将基数传递给 parseInt 而不是 fromCharCode
    【解决方案2】:

    String.fromCharCode("0x" + input)

    String.fromCharCode(parseInt(input, 16)) 因为它们是 16 位数字 (UTF-16)

    【讨论】:

      【解决方案3】:

      JavaScript uses UCS-2 internally.

      因此,String.fromCharCode(codePoint) 不适用于补充 Unicode 字符。例如,如果codePoint1195580x1D306,对于'?' 字符)。

      如果您想创建基于非 BMP Unicode 码位的字符串,您可以使用 Punycode.js 的实用函数在 UCS-2 字符串和 UTF-16 码位之间进行转换:

      // `String.fromCharCode` replacement that doesn’t make you enter the surrogate halves separately
      punycode.ucs2.encode([0x1d306]); // '?'
      punycode.ucs2.encode([119558]); // '?'
      punycode.ucs2.encode([97, 98, 99]); // 'abc'
      

      【讨论】:

        【解决方案4】:
        var hex = '2122';
        var char = unescape('%u' + hex);
        
        console.log(char);
        

        将返回“™”

        【讨论】:

          【解决方案5】:

          从 ES5 开始就可以使用

          String.fromCodePoint(number)

          获取大于 0xFFFF 的 unicode 值。

          所以,在每一个新的浏览器中,你都可以这样写:

          var input = '2122';
          console.log(String.fromCodePoint(input));
          

          或者如果是十六进制数:

          var input = '2122';
          console.log(String.fromCodePoint(parseInt(input, 16)));
          

          更多信息:

          https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint

          编辑(2021 年):

          fromCodePoint 不仅用于更大的数字,还用于组合 Unicode 表情符号。 例如,要画一只挥手的手,你必须写:

          String.fromCodePoint(0x1F44B);
          

          但如果你想要一只带有肤色的挥手,你必须将它结合起来:

          String.fromCodePoint(0x1F44B, 0x1F3FC);
          

          在未来(或从现在开始),您甚至可以将 2 个表情符号组合成一个新的表情符号,例如心脏和火,以创建燃烧的心脏:

          String.fromCodePoint(0x2764, 0xFE0F, 0x200D, 0x1F525);
          

          32-bit number:
          <script>
          document.write(String.fromCodePoint(0x1F44B));
          </script>
          <br>
          32-bit number + skin:
          <script>
          document.write(String.fromCodePoint(0x1F44B, 0x1F3FE));
          </script>
          <br>
          32-bit number + another emoji:
          <script>
          document.write(String.fromCodePoint(0x2764, 0xFE0F, 0x200D, 0x1F525));
          </script>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-03-04
            • 1970-01-01
            • 1970-01-01
            • 2017-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多