javascript problam中的unicode与fromCharCode一起添加值

unicode in javascript problam with fromCharCode while adding value

本文关键字:一起 添加 fromCharCode problam 中的 unicode javascript      更新时间:2023-09-26

fromCharCode函数用于语言消息转换,但其nt仅在像这样使用时才起作用

String.fromCharCode(2991, 3006, 2996, 3009, 2990, 2994, 2991, 2985, 3015)

但如果放入字符串或数组,则根本不起作用,请指导我

 var n1 = '2991,3006,2996,3009,2990,2994,2991,2985,3015';
 var numberArray = [2991,3006,2996,3009,2990,2994,2991,2985,3015];
 var n=String.fromCharCode(numberArray);
 alert(n);

由于String.fromCharCodearguments上工作,因此您首先提供了正确的定义。如果你需要使用其他的,你必须使用apply:

var arr = [2991,3006,2996,3009,2990,2994,2991,2985,3015],
    converted = String.fromCharCode(String, a);

或者,如果您需要使用字符串,只需将其拆分为一个数组,然后使用上面的方法apply即可。

var str = '2991,3006,2996,3009,2990,2994,2991,2985,3015',
    arr = str.split(','),
    converted = String.fromCharCode(String, a);

一个非常小的基本功能,涵盖了所有这些:

function convert() {
    var arr = [],
        arg = arguments[0];
    if(typeof arg === 'number') { // assume all args are numbers, split them into the array
        arr = Array.prototype.splice.call(arguments, 0);
    } else if(typeof arg === 'string') { // string, let's split it into an array!
        arr = arg.split(',');
    } else if(typeof arg === 'object') { //typeof returns object, but it should be an array
        arr = arguments[0];
    }
    return String.fromCharCode.apply(String, arr);
}

像使用convert('2991,3006,2996,3009,2990,2994,2991,2985,3015')convert([2991,3006,2996,3009,2990,2994,2991,2985,3015])一样使用它,将返回相同的结果。