node.js中的buffer.Buffer.byteLength方法使用说明
方法说明:
获取字符串的字节长度。
这个函数与String.prototype.length不同点在于,后者返回的是字符串的字符数。
语法:
Buffer.byteLength(string,[encoding])
接收参数:
string 字符创
encoding 字符串编码,默认为‘utf8′
例子:
str='\u00bd+\u00bc=\u00be'; console.log(str+":"+str.length+"characters,"+ Buffer.byteLength(str,'utf8')+"bytes"); //½+¼=¾:9characters,12bytes
源码:
Buffer.byteLength=function(str,enc){
varret;
str=str+'';
switch(enc){
case'ascii':
case'binary':
case'raw':
ret=str.length;
break;
case'ucs2':
case'ucs-2':
case'utf16le':
case'utf-16le':
ret=str.length*2;
break;
case'hex':
ret=str.length>>>1;
break;
default:
ret=internal.byteLength(str,enc);
}
returnret;
};
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短