这个是之前为写一个截取HTML段方法,而扩展的一个根据字节数截取字符串的原型方法。
想想之前的算法真罗嗦,简化了一下:
String.prototype.sliceByte = function(byteLength, ceil){
var i = 0, bL = 0;
while(bL < byteLength)
bL += this.charCodeAt(i++) > 127 ? 2 : 1;
return this.slice(0, bL > byteLength && ceil ? (i-1) : i);
};
想想之前的算法真罗嗦,简化了一下:
String.prototype.sliceByte = function(byteLength, ceil){
var i = 0, bL = 0;
while(bL < byteLength)
bL += this.charCodeAt(i++) > 127 ? 2 : 1;
return this.slice(0, bL > byteLength && ceil ? (i-1) : i);
};