js判断字符串长度

xiaoxiao2025-04-29  9

参考: https://www.cnblogs.com/hello321/p/7821400.html

function countLength(str) { var inputLength = 0; //给一个变量来记录长度 for (var i = 0; i < str.length; i++) { var countCode = str.charCodeAt(i); console.log("countCode打印:",countCode); //返回指定位置的字符的Unicode编码 //判断是不是ASCII码,Unicode码前128个字符是ASCII码 if (countCode >= 0 && countCode <= 128) { inputLength++; } else { inputLength += 2; //如果是扩展码,则一次+2 } } return inputLength; } console.log(countLength(",.?!@ a bczAZ喊,。?!"));
转载请注明原文地址: https://www.6miu.com/read-5029441.html

最新回复(0)