js字符串截取函数(中文2个位置,英文1个位置)

xiaoxiao2024-12-21  8

function countCharacters(str,size){ var totalCount = 0; var newStr = ""; for (var i=0; i<str.length; i++) { var c = str.charCodeAt(i); if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { totalCount++; }else { totalCount+=2; } if(totalCount<size){ newStr = str.substring(0,i+1); }else{ return newStr+"..."; } } return newStr; }
转载请注明原文地址: https://www.6miu.com/read-5021627.html

最新回复(0)