Posted on 2015-10-31 17:35
lxasp 阅读(1414)
评论(0) 编辑 收藏 引用 所属分类:
AJAX
var chsSplitSize = function(inputstr,size) {
var inarr = inputstr.split(""),strcurr,strnext,asccurr,ascnext,posonebase=0,lastzerobase=0,chsnext=false,i,j,k=0,ou=[],s;
inarr.push("");
for(i=0;i<inarr.length-1;i++){
strcurr=inarr[i];strnext=inarr[i+1];
try {
asccurr = strcurr.charCodeAt(0);
ascnext = strnext.charCodeAt(0);
} catch (e) {};
if (asccurr > 255){
posonebase+=2;
}else{
posonebase++;
};
if (ascnext > 255){
chsnext=true;
}else{
chsnext=false;
};
if(chsnext&&(posonebase-lastzerobase)==(size-1)){
s="";
for(j=k;j<=i;j++){
s+=inarr[j];
}
k=i+1;
lastzerobase=posonebase;
if(s.length>0)ou.push(s);
}else if((posonebase-lastzerobase)==size){
s="";
for(j=k;j<=i;j++){
s+=inarr[j];
}
k=i+1;
lastzerobase=posonebase;
if(s.length>0)ou.push(s);
}
}
if((posonebase-lastzerobase)<=size){
s="";
for(j=k;j<=i;j++){
s+=inarr[j];
}
k=i+1;
lastzerobase=posonebase;
if(s.length>0)ou.push(s);
}
return ou;
};
// 123456789012345678901
var s="",arr=chsSplitSize("一二三234我的额1额56789四五六七",12);
for(var i=0;i<arr.length;i++){
s+='<div>'+arr[i]+'</div>\n';
}
alert(s);