buf.lastIndexOf(value[, byteOffset][, encoding])
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
而不是第一次出现。
const buf = Buffer.from('this buffer is a buffer');
console.log(buf.lastIndexOf('this'));
// 打印: 0
console.log(buf.lastIndexOf('buffer'));
// 打印: 17
console.log(buf.lastIndexOf(Buffer.from('buffer')));
// 打印: 17
console.log(buf.lastIndexOf(97));
// 打印: 15 (97 是 'a' 的十进制 ASCII 值)
console.log(buf.lastIndexOf(Buffer.from('yolo')));
// 打印: -1
console.log(buf.lastIndexOf('buffer', 5));
// 打印: 5
console.log(buf.lastIndexOf('buffer', 4));
// 打印: -1
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'utf16le');
console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'utf16le'));
// 打印: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'utf16le'));
// 打印: 4
如果 value
不是字符串、数字或 Buffer
,则此方法将抛出 TypeError
。
如果 value
是数字,则它将被强制转换为有效的字节值(0 到 255 之间的整数)。
如果 byteOffset
不是数字,则会被强制为数字。
任何强制到 NaN
的参数,如 {}
或 undefined
,都将搜索整个缓冲区。
此行为与 String#lastIndexOf()
匹配。
const b = Buffer.from('abcdef');
// 传入数字值,但不是有效字节。
// 打印: 2, 相当于搜索 99 或 'c'。
console.log(b.lastIndexOf(99.9));
console.log(b.lastIndexOf(256 + 99));
// 传入强制为 NaN 的 byteOffset。
// 打印: 1, 搜索整个缓冲区。
console.log(b.lastIndexOf('b', undefined));
console.log(b.lastIndexOf('b', {}));
// 传递强制为 0 的 byteOffset。
// 打印: -1, 相当于传入 0。
console.log(b.lastIndexOf('b', null));
console.log(b.lastIndexOf('b', []));
如果 value
为空字符串或空 Buffer
,则返回 byteOffset
。
value
<string> | <Buffer> | <Uint8Array> | <integer> What to search for.byteOffset
<integer> Where to begin searching inbuf
. If negative, then offset is calculated from the end ofbuf
. Default:buf.length - 1
.encoding
<string> Ifvalue
is a string, this is the encoding used to determine the binary representation of the string that will be searched for inbuf
. Default:'utf8'
.- Returns: <integer> The index of the last occurrence of
value
inbuf
, or-1
ifbuf
does not containvalue
.
Identical to buf.indexOf()
, except the last occurrence of value
is found
rather than the first occurrence.
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
If value
is not a string, number, or Buffer
, this method will throw a
TypeError
. If value
is a number, it will be coerced to a valid byte value,
an integer between 0 and 255.
If byteOffset
is not a number, it will be coerced to a number. Any arguments
that coerce to NaN
, like {}
or undefined
, will search the whole buffer.
This behavior matches String#lastIndexOf()
.
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', []));
If value
is an empty string or empty Buffer
, byteOffset
will be returned.