码迷,mamicode.com
首页 > Web开发 > 详细

js获取字符串的字节长度

时间:2018-08-02 11:25:01      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:tle   +=   获取   输出   count   turn   col   new   har   

    //获得输入框中字符长度
    function getLength(val) {  
        var str = new String(val);  
        var bytesCount = 0;  
        for (var i = 0 ,n = str.length; i < n; i++) {  
            var c = str.charCodeAt(i);  
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {  
                bytesCount += 1;  
            } else {  
                bytesCount += 2;  
            }  
        }  
        return bytesCount;  
    }  
var str="你好"

getLength(str)

输出:4

 

js获取字符串的字节长度

标签:tle   +=   获取   输出   count   turn   col   new   har   

原文地址:https://www.cnblogs.com/Web-Architecture/p/9405635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!