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


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

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

曆史
版本變化
v8.0.0

value 現在可以是 Uint8Array

v5.7.0、v4.4.0

傳遞encoding 時,不再需要byteOffset 參數。

v1.5.0

添加於:v1.5.0


參數
  • value <string> | <Buffer> | <Uint8Array> | <integer> 要搜索的內容。
  • byteOffset <integer>buf 中從哪裏開始搜索。如果為負,則從 buf 的末尾計算偏移量。 默認: 0
  • encoding <string> 如果 value 是字符串,則這是用於確定將在 buf 中搜索的字符串的二進製表示形式的編碼。 默認: 'utf8'
  • 返回: <integer> buf 中第一次出現 value 的索引,如果 buf 不包含 value ,則返回 -1

如果value 是:

  • 一個字符串 value 根據 encoding 中的字符編碼進行解釋。
  • a Buffer Uint8Array value 將全部使用。要比較部分 Buffer ,請使用 buf.subarray
  • 一個數字 value 將被解釋為 0255 之間的無符號 8 位整數值。
import { Buffer } from 'node:buffer';

const buf = Buffer.from('this is a buffer');

console.log(buf.indexOf('this'));
// Prints: 0
console.log(buf.indexOf('is'));
// Prints: 2
console.log(buf.indexOf(Buffer.from('a buffer')));
// Prints: 8
console.log(buf.indexOf(97));
// Prints: 8 (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(Buffer.from('a buffer example')));
// Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
// Prints: 8

const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');

console.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
// Prints: 4
console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
// Prints: 6const { Buffer } = require('node:buffer');

const buf = Buffer.from('this is a buffer');

console.log(buf.indexOf('this'));
// Prints: 0
console.log(buf.indexOf('is'));
// Prints: 2
console.log(buf.indexOf(Buffer.from('a buffer')));
// Prints: 8
console.log(buf.indexOf(97));
// Prints: 8 (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(Buffer.from('a buffer example')));
// Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
// Prints: 8

const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');

console.log(utf16Buffer.indexOf('\u03a3', 0, 'utf16le'));
// Prints: 4
console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le'));
// Prints: 6

如果 value 不是字符串、數字或 Buffer ,則此方法將拋出 TypeError 。如果value 是一個數字,它將被強製轉換為一個有效的字節值,一個介於 0 和 255 之間的整數。

如果byteOffset 不是數字,它將被強製轉換為數字。如果強製的結果是 NaN0 ,則將搜索整個緩衝區。此行為與 String.prototype.indexOf() 匹配。

import { Buffer } from 'node:buffer';

const b = Buffer.from('abcdef');

// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.indexOf(99.9));
console.log(b.indexOf(256 + 99));

// Passing a byteOffset that coerces to NaN or 0.
// Prints: 1, searching the whole buffer.
console.log(b.indexOf('b', undefined));
console.log(b.indexOf('b', {}));
console.log(b.indexOf('b', null));
console.log(b.indexOf('b', []));const { Buffer } = require('node:buffer');

const b = Buffer.from('abcdef');

// Passing a value that's a number, but not a valid byte.
// Prints: 2, equivalent to searching for 99 or 'c'.
console.log(b.indexOf(99.9));
console.log(b.indexOf(256 + 99));

// Passing a byteOffset that coerces to NaN or 0.
// Prints: 1, searching the whole buffer.
console.log(b.indexOf('b', undefined));
console.log(b.indexOf('b', {}));
console.log(b.indexOf('b', null));
console.log(b.indexOf('b', []));

如果 value 為空字符串或空 BufferbyteOffset 小於 buf.length ,則將返回 byteOffset。如果 value 為空且 byteOffset 至少為 buf.length ,則將返回 buf.length

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 buf.indexOf(value[, byteOffset][, encoding])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。