搜索插件、代码仓与 APP 分类

插件详情v7.0.0

sprintf

sprintf 的 Dart 实现。提供简单的 printf 样式格式化,例如 sprintf("hello %s", ["world"]);

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
947万
喜欢数
139
Pub 分数
130

安装插件

flutter pub add sprintf

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 实现的 sprintf 函数,支持 printf 风格的字符串格式化。它允许开发者使用 %s、%d 和 %f 等占位符创建格式化字符串,并通过列表传递值。该包轻量且非常适合在跨平台环境中生成动态消息、日志或错误输出,保持一致的格式化风格。它支持所有主要的 Flutter 平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 格式化日志消息
  • 生成动态错误消息
  • 创建用户友好的输出字符串
  • 生成调试信息
  • 带控制的字符串插值
  • 基于模板的文本生成

主要特点

  • printf 样式的格式化语法
  • 支持 %s、%d、%f 占位符
  • 简单的 API,只需一次函数调用
  • 跨平台兼容性
  • 轻量且依赖极少
  • 通过 pub add 轻松集成

适合对象

  • 需要 printf 类格式化的开发人员
  • 日志和调试工具
  • 国际化就绪的字符串模板
  • 需要一致字符串输出的应用程序
  • 偏好 C 风格格式的团队
  • 简单文本生成需求

使用建议

  • 不支持高级格式设置选项
  • 仅限基本占位符类型
  • 无内置本地化支持
  • 不适合复杂的国际化字符串
  • 需要手动处理类型转换
  • 可能不如命名插值可读

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

dart-sprintf

Dart implementation of sprintf.

Build Status

ChangeLog

ChangeLog.md

Getting Started

Add the following to your pubspec.yaml:

dependencies:
  sprintf: "^7.0.0"

then run pub install.

Next, import dart-sprintf:

import 'package:sprintf/sprintf.dart';
Example
import 'package:sprintf/sprintf.dart';

void main() {
	print(sprintf("%04i", [-42]));
	print(sprintf("%s %s", ["Hello", "World"]));
	print(sprintf("%#04x", [10]));
}
-042
Hello World
0x0a

Limitations

  • Negative numbers are wrapped as 64bit ints when formatted as hex or octal.

Differences to C's printf

  • When using fixed point printing of numbers with large exponents, C introduces errors after 20 decimal places. Dart-printf will just print 0s.

所属分类

源码仓库

使用此插件的项目