1. 方法一: 是这样的

 

function strlen(str){  
    var len = 0;  
    for (var i=0; i<str.length; i++)="" {="" var="" c="str.charCodeAt(i);" 单字节加1="" if="" ((c="">= 0x0001 &amp;&amp; c &lt;= 0x007e) || (0xff60&lt;=c &amp;&amp; c&lt;=0xff9f)) {   
       len++;   
     }   
     else {   
      len+=2;   
     }   
    }   
    return len;  
}</str.length;>

2. 方法二:

 

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;    
}</this.length;>

 

3. 方法三:

把双字节的替换成两个单字节的然后再获得长度

getBLen = function(str) {  
    if (str == null) return 0;  
    if (typeof str != "string"){  
        str += "";  
    }  
    return str.replace(/[^\x00-\xff]/g,"01").length;
}

 

4.方法四:

var l = str.length; 
var blen = 0; 
for(i=0; i<l;i++){

if ((str.charCodeAt(i) &amp; 0xff00) != 0) { 
blen ++; 
} 
blen ++; 
}

5.方法5:

var jmz = {};  
jmz.GetLength = function(str) {  
    ///<summary>获得字符串实际长度,中文2,英文1</summary>  
    ///<param name="str">要获得长度的字符串  
    var realLength = 0, len = str.length, charCode = -1;  
    for (var i = 0; i &lt; len; i++) {  
        charCode = str.charCodeAt(i);  
        if (charCode &gt;= 0 &amp;&amp; charCode &lt;= 128) realLength += 1;  
        else realLength += 2;  
    }  
    return realLength;  
};

觉得可用,就经常来吧! js判断字符串长度就这样了,欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!