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
。
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> | <Uint8Array> | <integer> What to search for.byteOffset
<integer> Where to begin searching inbuf
. If negative, then offset is calculated from the end ofbuf
. Default:0
.encoding
<string> Ifvalue
is a string, this is its encoding. Default:'utf8'
.- Returns: <boolean>
true
ifvalue
was found inbuf
,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