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


  • value <string> | <Buffer> | <integer> 要查找的值。
  • byteOffset <integer> buf 中开始查找的偏移量。默认为 0
  • encoding <string> 如果 value 是字符串,则指定 value 的字符编码。默认为 'utf8'
  • 返回: <boolean> 如果 buf 查找到 value,则返回 true,否则返回 false

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

const buf = Buffer.from('this is a buffer');

console.log(buf.includes('this'));
// 输出: true
console.log(buf.includes('is'));
// 输出: true
console.log(buf.includes(Buffer.from('a buffer')));
// 输出: true
console.log(buf.includes(97));
// 输出: true(97 是 'a' 的十进制 ASCII 值)
console.log(buf.includes(Buffer.from('a buffer example')));
// 输出: false
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
// 输出: true
console.log(buf.includes('this', 4));
// 输出: false
  • value <string> | <Buffer> | <integer> What to search for.
  • byteOffset <integer> Where to begin searching in buf. Default: 0.
  • encoding <string> If value is a string, this is its encoding. Default: 'utf8'.
  • Returns: <boolean> true if value was found in buf, false otherwise.

Equivalent to buf.indexOf() !== -1.

const buf = Buffer.from('this is a buffer');

console.log(buf.includes('this'));
// Prints: true
console.log(buf.includes('is'));
// Prints: true
console.log(buf.includes(Buffer.from('a buffer')));
// Prints: true
console.log(buf.includes(97));
// Prints: true (97 is the decimal ASCII value for 'a')
console.log(buf.includes(Buffer.from('a buffer example')));
// Prints: false
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));
// Prints: true
console.log(buf.includes('this', 4));
// Prints: false