当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Buffer buf.includes(value[, byteOffset][, encoding])用法及代码示例


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

添加于:v5.3.0

参数
  • value <string> | <Buffer> | <Uint8Array> | <integer> 要搜索的内容。
  • byteOffset <integer>buf 中从哪里开始搜索。如果为负,则从 buf 的末尾计算偏移量。 默认: 0
  • encoding <string> 如果value 是一个字符串,这就是它的编码。 默认: 'utf8'
  • 返回: <boolean> true 如果在 buf 中找到 value,否则为 false

等效于 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

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 buf.includes(value[, byteOffset][, encoding])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。