var jmz ={};
jmz.GetLength=function(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;};alert(jmz.GetLength('測試測試ceshiceshi));
方法二:
var l = str.length;var blen =0;for(i=0; i<l; i++){if((str.charCodeAt(i)&0xff00)!=0){
blen ++;}
blen ++;}
方法三:
var jmz ={};
jmz.GetLength=function(str){return str.replace(/[\u0391-\uFFE5]/g,"aa").length;//先把中文替換成兩個字節的英文,在計算長度};alert(jmz.GetLength('測試測試ceshiceshi'));
js判斷字符串長度
方法1:
String.prototype.gblen=function(){var len =0;for(var i=0; i<this.length; i++){if(this.charCodeAt(i)>127||this.charCodeAt(i)==94){
len +=2;}else{
len ++;}}return len;}
方法2:
functionstrlen(str){var len =0;for(var i=0; i<str.length; i++){var c = str.charCodeAt(i);//單字節加1 if((c >=0x0001&& c <=0x007e)||(0xff60<=c && c<=0xff9f)){
len++;}else{
len+=2;}}return len;}
方法3:
var jmz ={};
jmz.GetLength=function(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;};
方法4:
var l = str.length;var blen =0;for(i=0; i<l; i++){if((str.charCodeAt(i)&0xff00)!=0){
blen ++;}
blen ++;}