写一个js 方法 输出中文字符编码的长度
function getChineseLength(str) { var len = 0; for (var i = 0; i < str.length; i++) { var code = str.charCodeAt(i); if (code >= 0x4e00 && code <= 0x9fff) { len += 2; } else { len += 1; } } return len; }
// 示例: var str = "中文字符编码"; console.log(getChineseLength(str)); // 16
原文地址: https://www.cveoy.top/t/topic/hiub 著作权归作者所有。请勿转载和采集!