方法说明:
获取字符串的字节长度。
这个函数与 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");
// ½ + ¼ = ¾: 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;
};
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn