String.prototype.charCodeAt()和String.fromCharCode()

String.prototype.charCodeAt()

charCodeAt()方法会返回字符所代表的unicode编码,返回值是0~65535之间的整数。

charCodeAt()只能处理基础平面的字符。

"ABC".charCodeAt(0) // returns 65:"A"

"ABC".charCodeAt(1) // returns 66:"B"

"ABC".charCodeAt(2) // returns 67:"C"

"ABC".charCodeAt(3) // returns NaN

修复charCodeAt()对于未知非基础平面字符

下面这个方法放在for循环里面使用,最后可以返回转换过的辅助平面编码。

function fixedCharCodeAt (str, idx) {//str字符串,idx字符索引
    idx = idx || 0;
    var code = str.charCodeAt(idx);//获取idx处字符的unicode编码
    var hi, low;
    
    if (0xD800 <= code && code <= 0xDBFF) {//如果处于高半区
        hi = code;
        low = str.charCodeAt(idx+1);//低半区就是紧接着的字符
        if (isNaN(low)) {//如果不存在低半区字符就抛出错误
            throw 'High surrogate not followed by low surrogate in fixedCharCodeAt()';
        }
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;//根据转换公式转换成辅助平面编码然后返回
    }
    if (0xDC00 <= code && code <= 0xDFFF) {//如果是低半区返回,因为这个方法可以在for循环里使用,前面的循环已经处理对应的高半区了
        return false;
        /*hi = str.charCodeAt(idx-1);
        low = code;
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;*/
    }
    return code;
}

修复已知的非基础平面字符

 

function knownCharCodeAt (str, idx) {
    str += '';
    var code,
        end = str.length;

    var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
    while ((surrogatePairs.exec(str)) != null) {
        var li = surrogatePairs.lastIndex;//正则的下一次匹配的起始索引
        if (li - 2 < idx) {//这里要寻找高半区字符,如果idx是低半区的低半区字符就+1跳过
            idx++;
        }
        else {
            break;
        }
    }

    if (idx >= end || idx < 0) {
        return NaN;
    }

    code = str.charCodeAt(idx);

    var hi, low;
    if (0xD800 <= code && code <= 0xDBFF) {
        hi = code;
        low = str.charCodeAt(idx+1);
        // Go one further, since one of the "characters" is part of a surrogate pair
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
    }
    return code;
}

 

String.fromCharCode()

fromCharCode()方法返回使用指定的Unicode值序列创建的字符串。

String.fromCharCode(65,66,67);
//返回字符串'ABC'

这个方法只能处理基础平面的unicode字符,如果需要处理辅助平面的字符,可以使用ES6的String.fromCodePoint()。

 

posted @ 2018-06-21 14:39  hahazexia  阅读(785)  评论(0)    收藏  举报