buf.lastIndexOf(value[, byteOffset][, encoding])
曆史
版本 | 變化 |
---|---|
v8.0.0 |
|
v6.0.0 | 添加於:v6.0.0 |
參數
value
<string> | <Buffer> | <Uint8Array> | <integer> 要搜索的內容。byteOffset
<integer> 在buf
中從哪裏開始搜索。如果為負,則從buf
的末尾計算偏移量。 默認:buf.length - 1
。encoding
<string> 如果value
是字符串,則這是用於確定將在buf
中搜索的字符串的二進製表示形式的編碼。 默認:'utf8'
。- 返回: <integer>
buf
中最後一次出現value
的索引,如果buf
不包含value
,則返回-1
。
與
相同,除了找到最後一次出現的 buf.indexOf()
value
而不是第一次出現。
import { Buffer } from 'node:buffer'; const buf = Buffer.from('this buffer is a buffer'); console.log(buf.lastIndexOf('this')); // Prints: 0 console.log(buf.lastIndexOf('buffer')); // Prints: 17 console.log(buf.lastIndexOf(Buffer.from('buffer'))); // Prints: 17 console.log(buf.lastIndexOf(97)); // Prints: 15 (97 is the decimal ASCII value for 'a') console.log(buf.lastIndexOf(Buffer.from('yolo'))); // Prints: -1 console.log(buf.lastIndexOf('buffer', 5)); // Prints: 5 console.log(buf.lastIndexOf('buffer', 4)); // Prints: -1 const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le'); console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le')); // Prints: 6 console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le')); // Prints: 4
const { Buffer } = require('node:buffer'); const buf = Buffer.from('this buffer is a buffer'); console.log(buf.lastIndexOf('this')); // Prints: 0 console.log(buf.lastIndexOf('buffer')); // Prints: 17 console.log(buf.lastIndexOf(Buffer.from('buffer'))); // Prints: 17 console.log(buf.lastIndexOf(97)); // Prints: 15 (97 is the decimal ASCII value for 'a') console.log(buf.lastIndexOf(Buffer.from('yolo'))); // Prints: -1 console.log(buf.lastIndexOf('buffer', 5)); // Prints: 5 console.log(buf.lastIndexOf('buffer', 4)); // Prints: -1 const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le'); console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le')); // Prints: 6 console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le')); // Prints: 4
如果 value
不是字符串、數字或 Buffer
,則此方法將拋出 TypeError
。如果value
是一個數字,它將被強製轉換為一個有效的字節值,一個介於 0 和 255 之間的整數。
如果byteOffset
不是數字,它將被強製轉換為數字。任何強製 NaN
的參數,如 {}
或 undefined
,都將搜索整個緩衝區。此行為與
匹配。String.prototype.lastIndexOf()
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.lastIndexOf(99.9)); console.log(b.lastIndexOf(256 + 99)); // Passing a byteOffset that coerces to NaN. // Prints: 1, searching the whole buffer. console.log(b.lastIndexOf('b', undefined)); console.log(b.lastIndexOf('b', {})); // Passing a byteOffset that coerces to 0. // Prints: -1, equivalent to passing 0. console.log(b.lastIndexOf('b', null)); console.log(b.lastIndexOf('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.lastIndexOf(99.9)); console.log(b.lastIndexOf(256 + 99)); // Passing a byteOffset that coerces to NaN. // Prints: 1, searching the whole buffer. console.log(b.lastIndexOf('b', undefined)); console.log(b.lastIndexOf('b', {})); // Passing a byteOffset that coerces to 0. // Prints: -1, equivalent to passing 0. console.log(b.lastIndexOf('b', null)); console.log(b.lastIndexOf('b', []));
如果 value
是空字符串或空 Buffer
,將返回 byteOffset
。
相關用法
- Node.js Buffer buf.length用法及代碼示例
- 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.indexOf(value[, byteOffset][, encoding])用法及代碼示例
- 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.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.lastIndexOf(value[, byteOffset][, encoding])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。