format
简介
CPP库: fmt
格式化语法: Format String Syntax
注意: pkg只封装了fmt中core.h的format接口,无法使用一些特殊的格式:如 计时规格 等
基本用法:
string result = format.format("Hello {}\n", "world"); // Hello world
format.print("Hello {}\n", "world"); // Hello world
更新
v1.1.0 支持了首字母大写、大小写转换功能:
format.format("Hello {0:c}\n", "world"); // Hello World
format.print("{:c}\n", "hello World"); // Hello World
format.format("Hello {0:u}\n", "world"); // Hello WORLD
format.format("Hello {0:l}\n", "world"); // Hello world
组件接口
format.gs
| 函数原型 | 函数作用 |
|---|---|
| string format(string fmt, ...) | 根据指定的格式将对象的值转换为字符串并插入到相应位置 |
| void print(string fmt, ...) | 直接打印结果 |
样例
// Accessing arguments by position:
write(format.format("{0}, {1}, {2}\n", "a", "b", 10));
// Result: "a, b, c"
write(format.format("{}, {}, {}\n", "a", "b", "c"));
// Result: "a, b, c"
write(format.format("{2}, {1}, {0}\n", "a", "b", "c"));
// Result: "c, b, a"
write(format.format("{0}{1}{0}\n\n", "abra", "cad")); // arguments' indices can be repeated
// Result: "abracadabra"
// Aligning the text and specifying a width:
write(format.format("{:<30}\n", "left aligned"));
// Result: "left aligned "
write(format.format("{:>30}\n", "right aligned"));
// Result: " right aligned"
write(format.format("{:^30}\n", "centered"));
// Result: " centered "
write(format.format("{:*^30}\n\n", "centered")); // use '*' as a fill char
// Result: "***********centered***********"