node.JS二进制操作模块buffer对象使用方法详解

2020-06-17 08:01:05易采站长站整理

// 输出: 15
// (97 是 'a' 的十进制 ASCII 值)
console.log(buf.lastIndexOf(97));
// 输出: -1
console.log(buf.lastIndexOf(Buffer.from('yolo')));
// 输出: 5
console.log(buf.lastIndexOf('buffer', 5));
// 输出: -1
console.log(buf.lastIndexOf('buffer', 4));

buf.includes(value[, byteOffset][, encoding])

该方法相当于 buf.indexOf() !== -1

value <String> | <Buffer> | <Integer> 要搜索的值

byteOffset <Integer> buf 中开始搜索的位置。默认: 0

encoding <String> 如果 value 是一个字符串,则这是它的字符编码。 默认: ‘utf8’

返回: <Boolean> 如果 buf 找到 value,则返回 true,否则返回 false


var buf = Buffer.from('this is a buffer');
// 输出: true
console.log(buf.includes('this'));
// 输出: true
console.log(buf.includes('is'));
// 输出: true
console.log(buf.includes(Buffer.from('a buffer')));
// 输出: true
// (97 是 'a' 的十进制 ASCII 值)
console.log(buf.includes(97));
// 输出: false
console.log(buf.includes(Buffer.from('a buffer example')));
// 输出: true
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
// 输出: false

更多关于nodeJS二进制操作模块buffer对象使用方法请查看下面的相关链接