node.js中的buffer.Buffer.byteLength方法使用說明
更新時間:2014年12月10日 10:03:27 投稿:junjie
這篇文章主要介紹了node.js中的buffer.Buffer.byteLength方法使用說明,本文介紹了buffer.Buffer.byteLength的方法說明、語法、接收參數、使用實例和實現源碼,需要的朋友可以參考下
方法說明:
獲取字符串的字節(jié)長度。
這個函數與 String.prototype.length 不同點在于,后者返回的是字符串的字符數。
語法:
復制代碼 代碼如下:
Buffer.byteLength(string, [encoding])
接收參數:
string 字符創(chuàng)
encoding 字符串編碼,默認為 ‘utf8′
例子:
復制代碼 代碼如下:
str = '\u00bd + \u00bc = \u00be';
console.log(str + ": " + str.length + " characters, " +
Buffer.byteLength(str, 'utf8') + " bytes");
// ½ + ¼ = ¾: 9 characters, 12 bytes
源碼:
復制代碼 代碼如下:
Buffer.byteLength = function(str, enc) {
var ret;
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);
}
return ret;
};
相關文章
Node.js發(fā)出請求走Proxyman代理調試tip詳解
這篇文章主要為大家介紹了Node.js發(fā)出請求走Proxyman代理調試tip詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08node?NPM庫string-random生成隨機字符串學習使用
這篇文章主要為大家介紹了node?NPM庫string-random生成隨機字符串學習使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07