请看原文: tryenough.com/flutter-str…
在Android和iOS平台都有相应的方法进行字符串的格式化,但是在flutter中却没有直接提供
在flutter上可以借助一个插件来进行格式化:
import 'package:sprintf/sprintf.dart';
复制代码
例子
import 'package:sprintf/sprintf.dart';
void main() {
print(sprintf("%04i", [-42]));
print(sprintf("%s %s", ["Hello", "World"]));
print(sprintf("%#04x", [10]));
double seconds = 5.0;
String name = 'Dilki';
List<String> pets = ['Cats', 'Dogs'];
String sentence1 = sprintf('Sends %2.2f seconds ago.', [seconds]);
String sentence2 = sprintf('Harry likes %s, I think %s likes %s.', [pets[0], name, pets[1]]);
print(sentence1);
print(sentence2);
}
复制代码