當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。