const str = '你是一个𠮷娃娃😻的小可爱'
console.log(str.slice(5,9)) //�娃娃�
console.log('😻'.length) //2
String.prototype.sliceByPoint = function(pStart,pEnd){
let result = '' //截取的结果
let pIndex = 0 //码点的指针
let cIndex = 0 //码元的指针
while(1){
if (pIndex >= pEnd || cIndex >= this.length) {
break
}
const point = this.codePointAt(cIndex) //point > 0xffff 大小决定是否占两个码元,codePointAt函数用于检索字符串中特定位置的字符的Unicode代码点
if(pIndex>=pStart){
result+=String.fromCodePoint(point)
}
pIndex++
cIndex += point > 0xffff ? 2:1
}
return result
}
console.log('111',str.sliceByPoint(5,8)) //娃娃😻