getting a string length that contains unicode character exceeding 0xffff

前端 未结 4 1630
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 06:23

I’m using this character, double sharp \'

4条回答
  •  庸人自扰
    2021-01-14 07:13

    That's the function I wrote to get string length in codepoint length

    function nbUnicodeLength(string){
        var stringIndex = 0;
        var unicodeIndex = 0;
        var length = string.length;
        var second;
        var first;
        while (stringIndex < length) {
    
            first = string.charCodeAt(stringIndex);  // returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
            if (first >= 0xD800 && first <= 0xDBFF && string.length > stringIndex + 1) {
                second = string.charCodeAt(stringIndex + 1);
                if (second >= 0xDC00 && second <= 0xDFFF) {
                    stringIndex += 2;
                } else {
                    stringIndex += 1;
                }
            } else {
                stringIndex += 1;
            }
    
            unicodeIndex += 1;
        }
        return unicodeIndex;
    }
    

提交回复
热议问题