简体   繁体   English

将 unicode 字符转换为字符串格式

[英]Converting unicode character to string format

有谁知道如何将 unicode 转换为 javascript 中的字符串。例如:

\∑ -> ∑<\/code> \2 -> 2<\/code> \∫ -> ∫<\/code>

我基本上希望能够在 xhtml 或 html 中显示符号。还没有决定我将使用哪个。

"

A function from k.ken's response: k.ken 响应中的一个函数:

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.获取输入字符串中的所有 unicode 字符,并将它们转换为字符。

Just found a way: String.fromCharCode(parseInt(unicode,16)) returns the right symbol representation.刚刚找到一种方法: String.fromCharCode(parseInt(unicode,16))返回正确的符号表示。 The unicode here doesn't have the \\u\u003c/code> in front of it just the number.这里的 unicode 前面没有\\u\u003c/code>只是数字。

To convert an given Unicode-Char like  to String-Representation, you can also use this one-liner:要将给定的 Unicode-Char(如 )转换为字符串表示,您还可以使用以下单行:

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

The above example gives you 'F21D'.上面的例子给你'F21D'。 Used with fontAwesome, you get street-view Icon: '\\F21D'与 fontAwesome 一起使用,您将获得街景图标:'\\F21D'

Another way:其他方式:

const unicodeText = "F1A3";
let unicodeChar = JSON.parse(`["\\u${unicodeText}"]`)[0];
var string = '/0004';  // One of unicode
var unicodeToStr = string.codePointAt(0).toString(16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM