javascript截取带有emoji表情的字符串
2020/08/08    标签: javascript    emoji字符串截取   
            String.prototype.substrEmoji = function () {
                var start = arguments[0] + (this.codePointAt(arguments[0] - 1) > 0xffff ? 1 : 0);
                var length = arguments[1] - (this.codePointAt(start + arguments[1] - 1) > 0xffff ? 1 : 0);
                return this.substr(start, length);
            }
            String.prototype.substrEmojiByte = function () {
                var strsArr = [];
                for (var str of this) {
                    strsArr.push(str);
                }
                return strsArr.splice(arguments[0], arguments[1]).join('');
            }
            String.prototype.substrEmojiByteArrayFrom = function () {
                return Array.from(this).splice(arguments[0], arguments[1]).join('');
            }
            String.prototype.lengthEmojiByte = function () {
                var strsArr = [];
                for (var str of this) {
                    strsArr.push(str);
                }
                return strsArr.length;
            }
var strs = "11";//这里可以是emoji字符串
                console.log(strs.substrEmoji(3, 4));
                console.log(strs.substrEmojiByte(2, 1));
                console.log(strs.lengthEmojiByte());