function chineseToGBKLength(str) { let len = 0; for (let i = 0; i < str.length; i++) { const charCode = str.charCodeAt(i); if (charCode <= 255) { len++; // 英文字符长度加1 } else { len += 2; // 中文字符长度加2 } } return len; }

// 示例 const chineseStr = '中国人民123abc'; const gbkLength = chineseToGBKLength(chineseStr); console.log(gbkLength); // 输出14


原文地址: https://www.cveoy.top/t/topic/oLmI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录