javascript获取字符串字节长度(转)

在 javascript下得到字符串的真正长度,即字节长度(字节数)

代码如下:

/**
 * description : 得到字符串的字节长度;
 * @author YangZY;
 * @version 0.2;
 * @return 返回字符串的字节长度(eg:"一二12"的字节长度是6);
 * 发布时间 : 2007.8.26;
 * 地点 : 哈尔滨;
 */
String.prototype.getLength=function(){
 var text=this.replace(/[^/x00-/xff]/g,"**");
 return text.length;
}

例子:

alert("一二三789".getLength());

返回值为9.

转自:http://blog.csdn.net/yzy0612/article/details/1759866

Leave a Reply