- assert断言
- async_hooks异步钩子
- async_hooks/context异步上下文
- buffer缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process子进程
- cluster集群
- CLI命令行
- console控制台
- Corepack核心包
- crypto加密
- crypto/webcrypto网络加密
- debugger调试器
- deprecation弃用
- dgram数据报
- diagnostics_channel诊断通道
- dns域名服务器
- domain域
- Error错误
- events事件触发器
- fs文件系统
- global全局变量
- http超文本传输协议
- http2超文本传输协议2.0
- https安全超文本传输协议
- inspector检查器
- Intl国际化
- module模块
- module/cjsCommonJS模块
- module/esmECMAScript模块
- module/package包模块
- net网络
- os操作系统
- path路径
- perf_hooks性能钩子
- permission权限
- policy安全策略
- process进程
- punycode域名代码
- querystring查询字符串
- readline逐行读取
- repl交互式解释器
- report诊断报告
- stream流
- stream/web网络流
- string_decoder字符串解码器
- test测试
- timers定时器
- tls安全传输层
- trace_events跟踪事件
- tty终端
- url网址
- util实用工具
- v8引擎
- vm虚拟机
- wasi网络汇编系统接口
- worker_threads工作线程
- zlib压缩
Node.js v18.15.0 文档
- Node.js 18.15.0
- ► 目录
-
►
索引
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS模块
- module/esm ECMAScript模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- policy 安全策略
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- ► 其他版本
- 文档搜索
目录
string_decoder 字符串解码器#
稳定性: 2 - 稳定
node:string_decoder 模块提供了用于将 Buffer 对象解码为字符串(以保留编码的多字节 UTF-8 和 UTF-16 字符的方式)的 API。
可以使用以下方式访问它:
const { StringDecoder } = require('node:string_decoder');
下面的示例展示了 StringDecoder 类的基本用法。
const { StringDecoder } = require('node:string_decoder');
const decoder = new StringDecoder('utf8');
const cent = Buffer.from([0xC2, 0xA2]);
console.log(decoder.write(cent));
const euro = Buffer.from([0xE2, 0x82, 0xAC]);
console.log(decoder.write(euro));
当 Buffer 实例被写入 StringDecoder 实例时,会使用内部的缓冲区来确保解码后的字符串不包含任何不完整的多字节字符。
这些都保存在缓冲区中,直到下一次调用 stringDecoder.write() 或调用 stringDecoder.end()。
在以下示例中,欧洲欧元符号 (€) 的三个 UTF-8 编码的字节通过三次单独的操作写入:
const { StringDecoder } = require('node:string_decoder');
const decoder = new StringDecoder('utf8');
decoder.write(Buffer.from([0xE2]));
decoder.write(Buffer.from([0x82]));
console.log(decoder.end(Buffer.from([0xAC])));
StringDecoder 类#
new StringDecoder([encoding])#
创建新的 StringDecoder 实例。
stringDecoder.end([buffer])#
buffer<Buffer> | <TypedArray> | <DataView> 包含要解码的字节的Buffer、TypedArray或DataView。- 返回: <string>
以字符串形式返回存储在内部缓冲区中的任何剩余的输入。 表示不完整的 UTF-8 和 UTF-16 字符的字节将被替换为适合字符编码的替换字符。
如果提供了 buffer 参数,则在返回剩余的输入之前执行对 stringDecoder.write() 的最后一次调用。
调用 end() 之后,stringDecoder 对象可以重新用于新的输入。
stringDecoder.write(buffer)#
buffer<Buffer> | <TypedArray> | <DataView> 包含要解码的字节的Buffer、TypedArray或DataView。- 返回: <string>
返回已解码的字符串,确保从返回的字符串中省略 Buffer、TypedArray 或 DataView 末尾的任何不完整的多字节字符,并将其存储在内部缓冲区中,以备下次调用 stringDecoder.write() 或 stringDecoder.end() 时使用。