我正在使用函数String.fromCharCode(decimal value),并将一个十进制值传递给它。
它的英文字符工作正常,但是当我尝试对日语字符进行解码时,它给了我一些仲裁字符。
谁能告诉我String.fromCharCode(decimal value)是否支持扩展字符。
最佳答案
不,它不支持使用两个替代字符的字符。 MDC具有用于处理此问题的实用程序功能:
// String.fromCharCode() alone cannot get the character at such a high code point
// The following, on the other hand, can return a 4-byte character as well as the
// usual 2-byte ones (i.e., it can return a single character which actually has
// a string length of 2 instead of 1!)
alert(fixedFromCharCode(0x2F804)); // or 194564 in decimal
function fixedFromCharCode (codePt) {
if (codePt > 0xFFFF) {
codePt -= 0x10000;
return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 +
(codePt & 0x3FF));
}
else {
return String.fromCharCode(codePt);
}
}