-
幕布斯6054654
不幸的是,至少我知道没有内置的方法。尝试这个function splitBufToArr(buf, splitStr) { let start = 0; let bufArr = []; let isfound = true; while (isfound) { const idx = buf.indexOf(splitStr, start); if (idx === -1) { isfound = false; if (bufArr.length > 0) bufArr.push(buf.slice(idx)); break; } const chunk = buf.slice(start, idx); start = idx + 1; bufArr.push(chunk); } return bufArr;}const buf = Buffer.from('1,5,8,9');console.log(splitBufToArr(buf, ','));
-
慕标琳琳
根据您的需要,您可能最好使用字符串,因为它具有分割功能。但是如果你确实想将东西保留为缓冲区而不是字符串,使用 slice 和 indexOf,你可以创建一个简单的 split 函数,..例如。function splitBuffer(b, splitWith) { const ret = []; let s = 0; let i = b.indexOf(splitWith, s); while (i >= 0) { if (i >= 0) { ret.push(b.slice(s, i)); } s = i + 1; i = b.indexOf(splitWith, s); } ret.push(b.slice(s)); return ret;}//testconst b = Buffer.from('one=two=three');console.log(splitBuffer(b, '='))//result = [ <Buffer 6f 6e 65>, <Buffer 74 77 6f>,//<Buffer 74 68 72 65 65> ]
-
www说
在下面的示例中,我们使用库iter-ops来处理Buffer可迭代对象:import {pipe, split} from 'iter-ops';const i = pipe( buffer, split(value => value === 0x3D) // 0x3D = code for '=');console.log([...i]); //=> split chunks of buffer