let a= new Buffer('abc');
let b = new Buffer('ddd');
let c = Buffer.concat([a,b]);
console.log(c.toString());
let d=new Buffer('abccc-=-dddder-=-qwerqwer');
console.log(d.indexOf('-=-'));
let e=new Buffer('abccc-=-dddder-=-qwerqwer');
console.log(e.slice(17).toString());
console.log(Buffer.split);
//扩展方法
let bb=new Buffer('abccc-=-dddder-=-qwerqwer');
Buffer.prototype.split=Buffer.prototype.split||function (b){
let arr=[];
let cur=0;
let n=0;
while((n=this.indexOf(b, cur))!=-1){//找分隔符b,从cur位置开始找,找到返回位置赋值给n
arr.push(this.slice(cur, n));
cur=n+b.length; //跳过分割符的位置
}
arr.push(this.slice(cur));
return arr;
};
let arr=bb.split('-=-');
console.log(arr);
console.log(arr.map(buffer=>buffer.toString()));
输出
abcddd
5
qwerqwer
undefined
[ <Buffer 61 62 63 63 63>,
<Buffer 64 64 64 64 65 72>,
<Buffer 71 77 65 72 71 77 65 72> ]
[ 'abccc', 'dddder', 'qwerqwer' ]
网友评论