• Node.js Video Tutorials

Node.js - Buffer.indexOf() 方法



NodeJS 的 Buffer.indexOf() 方法用於在緩衝區中搜索值。如果找到該值,則返回該值開始的位置;如果未找到,則返回 -1。如果該值在緩衝區中出現多次,則返回第一次出現的位置。

語法

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

buffer.indexOf(value, byteOffset, encoding);

引數

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

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

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

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

返回值

Buffer.indexOf() 方法將返回搜尋值第一次出現的位置。如果未找到該值,則返回 -1。

示例

在本例中,我們將嘗試在建立的緩衝區中搜索一個值並測試結果。

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

輸出

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

The string welcome is present in the buffer

示例

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

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

輸出

我們使用了 10 作為 byteOffset。它將從字串 "Welcome to TutorialsPoint" 的偏移量 10 開始搜尋。由於搜尋字串:Point 可用,因此您將看到輸出列印為 "字串 Point 存在於緩衝區中"。

The string point is present in the buffer

示例

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

const buffer = Buffer.from('Welcome to TutorialsPoint');
const result  = buffer.indexOf(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.indexOf() 方法中,我們使用了一個包含字串值:TutorialsPoint 的緩衝區。我們的主緩衝區包含字串:Welcome to TutorialsPoint。因此,由於該字串存在,因此返回的值是該字串的起始位置。

The string TutorialsPoint is present in the buffer

示例

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

const buffer = Buffer.from('Welcome to TutorialsPoint');
if (buffer.indexOf('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.indexOf() 中的編碼引數。

const buffer = Buffer.from('Hello World');
if (buffer.indexOf('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.indexOf(72) != -1) {
   console.log("The character H is present in the buffer");
} else {
   console.log("The string H is not present");
}

輸出

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

The string Hello is present in the buffer
nodejs_buffer_module.htm
廣告

© . All rights reserved.