• Node.js Video Tutorials

Node.js - Buffer.lastIndexOf() 方法



NodeJS 的 Buffer.lastIndexOf() 方法用於在緩衝區內搜尋某個值。它返回該值所在的位置,如果該值不存在則返回 -1。如果該值在緩衝區中出現多次,則返回最後出現的位置。

語法

以下是 NodeJS lastIndexOf() 方法的語法:

buffer.lastIndexOf(value, byteoffset, encoding);

引數

buffer.lastIndexOf() 方法接受三個引數。第一個引數是必填引數,其餘兩個引數是可選引數。

  • value − 這是必填引數。它是要在緩衝區內搜尋的值。該值可以是字串、數字或緩衝區。

  • byteOffset − 這是可選引數。它指定從哪裡開始搜尋。如果給出負偏移量,則將從緩衝區的末尾開始搜尋。預設值為 buffer.length-1。

  • encoding − 如果要搜尋的值是字串,則可以使用此引數指定編碼。這是一個可選引數。預設情況下使用的編碼是 utf8。

返回值

Buffer.lastIndexOf() 方法將返回它遇到的最後一個搜尋值的位置。如果該值不存在,則返回 -1。

示例

在此示例中,我們將嘗試在建立的緩衝區內搜尋某個值並測試結果。

const buffer = Buffer.from('Welcome to TutorialsPoint');
   
if (buffer.lastIndexOf('Welcome') != -1) {
   console.log("The string welcome is present in the buffer");
} else {
   console.log("The string welcome is not present");
}

輸出

我們正在使用字串“Welcome to Tutorialspoint”建立的緩衝區中搜索字串 Welcome。由於給定的字串存在,因此我們得到以下輸出。我們檢查該值是否不等於 -1。因為當我們在緩衝區中沒有找到給定值的匹配項時,會返回 -1。

The string welcome is present in the buffer

示例

讓我們嘗試在下面的示例中給出 byteOffset 引數:

const buffer = Buffer.from('Welcome to TutorialsPoint');
if (buffer.lastIndexOf('Point', -1) != -1) {
   console.log("The string point is present in the buffer");
} else {
   console.log("The string point is not present");
}

輸出

我們使用 -1 作為 byteOffset。它將從字串“Welcome to TutorialsPoint”的末尾開始搜尋。由於搜尋字串:Point 可用,您將獲得輸出列印為“字串 point 存在於緩衝區中”。

The string point is present in the buffer

示例

在此示例中,我們將使用緩衝區作為值來檢查它是否在給定的緩衝區內可用。

const buffer = Buffer.from('Welcome to TutorialsPoint');
const result  = buffer.lastIndexOf(Buffer.from('TutorialsPoint'));
if (result != -1) {
   console.log("The string TutorialsPoint is present in the buffer");
} else {
   console.log("The string TutorialsPoint is not present");
}

輸出

在 Buffer.lastIndexOf() 方法內部,我們使用了一個包含字串值:TutorialsPoint 的緩衝區。我們的主緩衝區包含一個字串:Welcome to TutorialsPoint。因此,由於該字串存在,返回的值是該字串的起始位置。

The string TutorialsPoint is present in the buffer

示例

讓我們嘗試一個錯誤情況,即檢查當字串不存在時 Buffer.lastIndexOf() 的響應。

const buffer = Buffer.from('Welcome to TutorialsPoint');   
if (buffer.lastIndexOf('Testing') != -1) {
   console.log("The string Testing is present in the buffer");
} else {
   console.log("The string Testing is not present");
}

輸出

我們正在使用字串:Welcome to TutorialsPoint 的緩衝區內搜尋字串:Testing。由於它不存在,它將返回 -1。因此,您將獲得輸出為:字串 Testing 不存在

The string Testing is not present

示例

讓我們在 Buffer.lastIndexOf() 中測試 encoding 引數。

const buffer = Buffer.from('Hello World');
if (buffer.lastIndexOf('Hello','hex') != -1) {
   console.log("The string Hello is present in the buffer");
} else {
   console.log("The string Hello is not present");
}

輸出

我們使用了“hex”編碼。當使用編碼進行檢查且字串存在時,它會返回字串的起始位置。

The string Hello is present in the buffer

示例

在此示例中,我們將使用整數(即 utf-8 編碼值)在緩衝區中進行搜尋。

const buffer = Buffer.from('Hello World');

if (buffer.lastIndexOf(72) != -1) {
   console.log("The character H is present in the buffer");
} else {
   console.log("The string H is not present");
}

輸出

這裡 72 根據 utf-8 編碼表示字元“H”。當在緩衝區中使用整數進行搜尋時,它會返回字元 H 的位置,因為它存在於緩衝區中。

The string Hello is present in the buffer

示例

讓我們檢查一個示例,其中同一個字串出現多次。

在示例中,我們使用 indexOf 和 lastIndexOf() 檢查位置。使用 lastIndexOf(),它給出最後出現的字串的位置,使用 indexOf(),它給出第一個出現的字串的位置。

const buffer = Buffer.from('She sells sea shells on the sea shore');
console.log("The position of sea using lastIndexOf() is "+ buffer.lastIndexOf("sea"));
console.log("The position of sea using indexOf() is "+ buffer.indexOf("sea"));

輸出

The position of sea using lastIndexOf() is 28
The position of sea using indexOf() is 10
nodejs_buffer_module.htm
廣告