字符长度超限生成新数组
/**
* 防止通讯录长度超出数据库存储长度4294967295
* */
if(GetLength(JSON.stringify(arr))>=4294967295){ console.log('长度超限'); var mydata = JSON.stringify(arr).substr(0 ,4294967295-2); console.log(mydata.substring(0,mydata.lastIndexOf(',{'))+']'); }else{ //JSON.stringify(arr)数据库存入 }
function GetLength(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary> ///<param name="str">要获得长度的字符串</param> 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; else realLength += 2; } return realLength; };