buf.includes(value[, byteOffset][, encoding])
value<string> | <Buffer> | <Uint8Array> | <integer> 要搜索的内容。byteOffset<integer> 在buf中开始搜索的位置。如果为负数,则偏移量从buf的末尾计算。默认值:0。encoding<string> 如果value是字符串,这是它的编码方式。默认值:'utf8'。- 返回值: <boolean> 如果在
buf中找到了value,则返回true,否则返回false。
等同于 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