buf.indexOf(value[, byteOffset][, encoding])
历史
| 版本 | 变化 |
|---|---|
| v8.0.0 |
|
| v5.7.0、v4.4.0 | 传递 |
| 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或,Uint8Arrayvalue将全部使用。要比较部分Buffer,请使用。buf.subarray - 一个数字
value将被解释为0和255之间的无符号 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 不是数字,它将被强制转换为数字。如果强制的结果是 NaN 或 0 ,则将搜索整个缓冲区。此行为与 匹配。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 为空字符串或空 Buffer 且 byteOffset 小于 buf.length ,则将返回 byteOffset。如果 value 为空且 byteOffset 至少为 buf.length ,则将返回 buf.length。
相关用法
- Node.js Buffer buf.includes(value[, byteOffset][, encoding])用法及代码示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代码示例
- Node.js Buffer buf.toString([encoding[, start[, end]]])用法及代码示例
- Node.js Buffer buf.writeDoubleLE(value[, offset])用法及代码示例
- Node.js Buffer buf.writeBigInt64LE(value[, offset])用法及代码示例
- Node.js Buffer buf.keys()用法及代码示例
- Node.js Buffer buf.writeFloatLE(value[, offset])用法及代码示例
- Node.js Buffer buf.readFloatLE([offset])用法及代码示例
- Node.js Buffer buf.readInt32LE([offset])用法及代码示例
- Node.js Buffer buf.writeInt8(value[, offset])用法及代码示例
- Node.js Buffer buf.swap32()用法及代码示例
- Node.js Buffer buf.writeInt32LE(value[, offset])用法及代码示例
- Node.js Buffer buf.writeIntLE(value, offset, byteLength)用法及代码示例
- Node.js Buffer buf.values()用法及代码示例
- Node.js Buffer buf.writeDoubleBE(value[, offset])用法及代码示例
- Node.js Buffer buf.length用法及代码示例
- Node.js Buffer buf.byteOffset用法及代码示例
- Node.js Buffer buf.readUInt32BE([offset])用法及代码示例
- Node.js Buffer buf.readDoubleLE([offset])用法及代码示例
- Node.js Buffer buf.writeFloatBE(value[, offset])用法及代码示例
- Node.js Buffer buf.readBigUInt64LE([offset])用法及代码示例
- Node.js Buffer buf.readUInt16BE([offset])用法及代码示例
- Node.js Buffer buf.writeInt16BE(value[, offset])用法及代码示例
- Node.js Buffer buf.swap64()用法及代码示例
- Node.js Buffer buf.readUInt8([offset])用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 buf.indexOf(value[, byteOffset][, encoding])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
