str="aklfjaoie"
alert(str.length)

解决方案 »

  1.   

    /*************************************************************
    名称:strbytelen
    功能:取得字符串的字节长度,汉字记两个长度
    参数:source,源字符串;
    返回:endvalue,字符串的字节长度
    *************************************************************/
    function strbytelen(source) {
    var endvalue=0;
    var sourcestr=new String(source);
    var tempstr;

    for (var strposition=0;strposition<=sourcestr.length-1;strposition++) {
    tempstr=sourcestr.substr(strposition,1);
    if (tempstr.charCodeAt(0)>255 || tempstr.charCodeAt(0)<0) {
    endvalue=endvalue+2;
    } else {
    endvalue=endvalue+1;
    }
    }
      
    return(endvalue);
    }
      

  2.   

    <script>
    String.prototype.len=function()
    {
        return this.replace(/[^\x00-\xff]/g,"**").length;
    }
    var str="我a";
    alert(str.len());
    </script>