stringDecoder.end()方法用於返回存儲在內部緩衝區中的所有剩餘輸入作為字符串。此方法確保將所有不完整的UTF-8和UTF-16字符替換為適合字符編碼的替換字符。
提供可選的buffer參數後,stringDecoder.write()
在返回剩餘的緩衝區輸入之前,對數據調用一次方法。
用法:
stringDecoder.end( [buffer] )
參數:該函數接受上述和以下描述的單個參數:
- buffer:它是一個Buffer,TypedArray或DataView,其中包含必須解碼的字節。它是一個可選參數。
返回值:它以字符串形式返回存儲在緩衝區中的其餘輸入。
以下示例程序旨在說明Node.js中的stringDecoder.end()方法:
範例1:
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// Using the end() method
const text_one = Buffer.from("GeeksforGeeks", "utf-8");
let decoded_text = decoder.end(text_one);
console.log("Decoded Text:", decoded_text);
// The Euro Symbol is denoted using
// the bytes [0xE2, 0x82, 0xAC]
console.log("Decoding the Euro Symbol:");
// Decoding the euro symbol
// Using the write() method to
// write the first two parts
console.log(decoder.write(Buffer.from([0xE2])));
console.log(decoder.write(Buffer.from([0x82])));
// Finishing the symbol using the end() method
// with that gives an additonal call to write()
// using the last part of the buffer
console.log(decoder.end(Buffer.from([0xAC])));
輸出:
Decoded Text:GeeksforGeeks Decoding the Euro Symbol: €
範例2:
// Import the string_decoder module
// and get the StringDecoder class
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// The Cent Symbol is denoted using
// Buffer.from([0xc2, 0xa2])
// Decoding the complete cent symbol from buffer
let cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.end(cent_symbol);
console.log("Complete Cent Symbol:", cent_symbol_out);
// Decoding incomplete cent symbol using
// Buffer.write() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol using write():",
cent_symbol_out);
// Decoding incomplete cent symbol
// using Buffer.end() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.end(cent_symbol);
console.log("Cent Symbol using end():",
cent_symbol_out);
輸出:
Complete Cent Symbol:¢ Cent Symbol using write(): Cent Symbol using end():??
參考: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_end_buffer
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM segment()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM raise()用法及代碼示例
- Node.js GM quality()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM roll()用法及代碼示例
- Node.js GM solarize()用法及代碼示例
- Node.js GM spread()用法及代碼示例
- Node.js GM motionBlur()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM shave()用法及代碼示例
- Node.js GM scale()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | stringDecoder.end() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。