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


  • value <string> | <Buffer> | <Uint8Array> | <integer> 要搜索的内容。

    ¥value <string> | <Buffer> | <Uint8Array> | <integer> What to search for.

  • byteOffset <integer> 开始搜索 buf 的位置。如果为负数,则从 buf 的末尾开始计算偏移量。默认值:0

    ¥byteOffset <integer> Where to begin searching in buf. If negative, then offset is calculated from the end of buf. Default: 0.

  • encoding <string> 如果 value 是字符串,则这就是它的编码。默认值:'utf8'

    ¥encoding <string> If value is a string, this is its encoding. Default: 'utf8'.

  • 返回:<boolean> 如果在 buf 中找到 value,则为 true,否则为 false

    ¥Returns: <boolean> true if value was found in buf, false otherwise.

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

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

import { Buffer } from 'node:buffer';

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: falseconst { Buffer } = require('node:buffer');

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