js计算中文字符串行数!——js遇到中文字符串长度怎么算?
Author:zhoulujun@live.cn Date:
js,算中文字符串,就,尼玛了……
<input maxlength=70/>
发现,明明不止70字节啊
因为,坑底的,中文占位啊!
不是2啊,不是2蛤,就是那么2!!
所以,你要写一个函数计算!!
function checkStringLength(str){
if(!str&&typeof (str)!=string){
return null;
}
var l=str.length;
var n=l;
while (l>0){
l--;
n+=str[l].charCodeAt()>255?1:0;
}
l=null;
return n;
}
console.log( checkString("ac1中"))
转载本站文章《js计算中文字符串行数!——js遇到中文字符串长度怎么算?》,
请注明出处:https://www.zhoulujun.cn/html/webfront/ECMAScript/js/2016_0314_7702.html