六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

字符串的字节数

[复制链接]

升级  28%

24

主题

24

主题

24

主题

秀才

Rank: 2

积分
92
 楼主| 发表于 2013-1-29 09:07:17 | 显示全部楼层 |阅读模式
<script type="text/javascript"><!--/**字符编码数值对应的存储长度:    UCS-2编码(16进制) UTF-8 字节流(二进制)   0000 - 007F       0xxxxxxx (1字节)    0080 - 07FF       110xxxxx 10xxxxxx (2字节)    0800 - FFFF       1110xxxx 10xxxxxx 10xxxxxx (3字节)   */String.prototype.getBytesLength = function() { var totalLength = 0;   var charCode;for (var i = 0; i < this.length; i++) {charCode = this.charCodeAt(i);if (charCode < 0x007f) {               totalLength++;   } else if (charCode <= 0x07ff) {               totalLength += 2;           } else if (charCode <= 0xffff) {               totalLength += 3;         } else{totalLength += 4; }        }return totalLength; }var str="my字符串образования";alert("字符数"+str.length+" ,字节数"+str.getBytesLength());  //Java中的字节数==="字符串".getBytes("UTF-8").length   //--></script> 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表