Converting unicode character to string format

后端 未结 5 2190
失恋的感觉
失恋的感觉 2020-11-30 07:44

Does anyone know how to convert a unicode to a string in javascript. For instance:

\\u2211 -> ∑ \\u0032 -> 2 \\u222B -> ∫

相关标签:
5条回答
  • 2020-11-30 07:52

    Just found a way: String.fromCharCode(parseInt(unicode,16)) returns the right symbol representation. The unicode here doesn't have the \u in front of it just the number.

    0 讨论(0)
  • 2020-11-30 07:52

    To convert an given Unicode-Char like  to String-Representation, you can also use this one-liner:

    var unicodeToStr = ''.codePointAt(0).toString(16)
    

    The above example gives you 'F21D'. Used with fontAwesome, you get street-view Icon: '\F21D'

    0 讨论(0)
  • 2020-11-30 07:57

    A function from k.ken's response:

    function unicodeToChar(text) {
       return text.replace(/\\u[\dA-F]{4}/gi, 
              function (match) {
                   return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16));
              });
    }
    

    Takes all unicode characters in the inputted string, and converts them to the character.

    0 讨论(0)
  • 2020-11-30 08:05
    var string = '/0004';  // One of unicode
    var unicodeToStr = string.codePointAt(0).toString(16)
    
    0 讨论(0)
  • 2020-11-30 08:07

    Another way:

    const unicodeText = "F1A3";
    let unicodeChar = JSON.parse(`["\\u${unicodeText}"]`)[0];
    
    0 讨论(0)
提交回复
热议问题