当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


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

历史
版本变化
v8.0.0

value 现在可以是 Uint8Array

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: 4const { 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

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 buf.lastIndexOf(value[, byteOffset][, encoding])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。