- 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 - 緩衝區 (Buffers)
- Node.js - 流 (Streams)
- 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 連線查詢 (Join)
- 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 資料限制 (Limit)
- Node.js - MongoDB 連線查詢 (Join)
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - Buffer.equals() 方法
NodeJS 的 Buffer.equals() 方法用於比較兩個緩衝區,如果它們相等則返回 true,否則返回 false。當它返回 0 時,它與 Buffer.compare() 方法有點類似。
語法
以下是 NodeJS equals() 方法的語法:
buffer.equals(buf);
引數
Buffer.equals() 方法接受一個引數,該引數也是一個緩衝區。這是一個必填引數。
buf − 用於比較的緩衝區物件。
返回值
如果比較的緩衝區相等,則 Buffer.equals() 返回 true;否則返回 false。
示例
在下面的示例中,我們建立了兩個包含字串“Hello”的緩衝區。之後,使用 NodeJS buffer.equal() 方法比較這兩個緩衝區。
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('Hello');
console.log("The output for buffer.equals() is "+buffer1.equals(buffer2));
輸出
The output for buffer.equals() is true
示例
在下面的示例中,我們建立了兩個緩衝區,分別包含字串“Hello”和“World”。之後,使用 buffer.equal() 方法比較這兩個緩衝區。由於字串不同,buffer.equals() 的輸出將為 false。
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('World');
console.log("The output for buffer.equals() is "+buffer1.equals(buffer2));
輸出
The output for buffer.equals() is false
示例
在下面的示例中,我們使用了 buffer.equals() 和 buffer.compare() 方法來比較字串是否相同。
const buffer1 = Buffer.from("hello");
var buffer2 = Buffer.from("hello");
buffer1.equals(buffer2); // It will return true
buffer1.compare(buffer2); // It will return 0
Buffer.compare(buffer1, buffer2);// It will return 0
console.log("Using Buffer.equals() "+ buffer1.equals(buffer2));
console.log("Using Buffer.compare() "+ buffer1.compare(buffer2));
輸出
Using Buffer.equals() true Using Buffer.compare() 0
示例
buffer.equals() 方法區分大小寫。這意味著字串 Hello 和 hello 是不同的。
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('hello');
console.log("The output for buffer.equals() is "+buffer1.equals(buffer2));
輸出
The output for buffer.equals() is false
示例
在下面的示例中,我們有兩個緩衝區,一個包含字串 Hello,另一個包含字串 Hello 的十六進位制程式碼值:48656c6c6f
當我們比較這兩個緩衝區時,它將返回 true。原因是兩者分配的空間相同。
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('48656c6c6f', 'hex');
console.log("The output for buffer.equals() is "+buffer1.equals(buffer2));
輸出
The output for buffer.equals() is true
nodejs_buffer_module.htm
廣告
