JSのデフォルトの漢字の長さは、他の文字長の計算方法と同じですが、場合によっては、中国の文字列の実際の長さを取得する必要があります。コードは次のとおりです。
コードコピーは次のとおりです。
function strlength(str)
{
var reallength = 0、len = str.length、charcode = -1;
for(var i = 0; i <len; i ++)
{
charcode = str.charcodeat(i);
if(charcode> = 0 && charcode <= 128)
{
Reallength += 1;
}
それ以外
{
Reallength += 2;
}
}
Return Reallength;
}