- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
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
