Preface
In daily development, string formatting is a commonly used function to help us beautify the output information.
Suppose there is such a text that needs to be formatted for output:
INFO 10000 --- [ main] AccountServiceApplication 执行时长 40 ms.
Formatting instructions
Attributes | Content description | format requirement |
---|---|---|
INFO | log level | Position 5, aligned to the left |
10000 | Process PID value | Position 6, displayed in the center |
main | Thread name | Position 15, aligned to the right |
AccountServiceApplication | Execution method name, occupancy 30, aligned to the right | |
40 | is the execution time value | Position 6, aligned to the left |
Java
The JDK comes with String.format()
no centering function, so we use Apache Common-Lang
the package to display it in the center.
import org.apache.commons.lang.StringUtils;
String line = "%-5s %s --- [%15s] %30s 执行时长 %-6d ms.";
System.out.println(
String.format(line,
"INFO",
StringUtils.center("10000",6),
"main",
"AccountServiceApplication",
40
)
);
Reference resources: Java formatted string-Usage of String.format()
Kotlin
Since Kotlin
it is Java
compatible with ., the above solution is also applicable to Kotlin
.
val line = "%-5s %s --- [%15s] %30s 执行时长 %-6d ms."
println(
line.format(
"INFO",
StringUtils.center("10000", 6),
"main",
"AccountServiceApplication",
40
)
)
Python
The following examples are based on
Python3
testing
python
Provides powerful format
methods for use {}
as placeholders, examples:
# 若 {} 中没有添加序号(0开始)则按照参数顺序
'Hello, my name is {}, {} years old!'.format("Tinker", 45)
# Hello, my name is Tinker, 45 years old!
# 同时可以任意调整顺序
'Hello, my name is {2}, {0} years old, I come from {1}'.format(45,"China","Tinker")
# Hello, my name is Tinker, 45 years old, I come from China
# 使用参数名或者字典(需要在前面加上 ** )
'Hello, my name is {name}, {age} years old!'.format(name="Tinker", age=45)
'Hello, my name is {name}, {age} years old!'.format(**{
"name":"Tinker", "age":45})
# Hello, my name is Tinker, 45 years old!
# 填充及格式化
# :[填充字符][对齐方式 <^> 分别是靠左、居中、靠右][宽度]
# 显示身份证后四位
'ID Num: {:*>18}'.format(5437)
# ID Num: **************5437
# 显示手机号码前三位
'Phone Num: {0:*<11}'.format(188)
# Phone Num: 188********
For 前言
the formatting requirements in , you can write like this:
'{level:<5} {pid:^6} --- [{thread:>15}] {method:>30} : 执行时长 {time:<6} ms'.format(level="INFO",pid=10000,thread="main",method="AccountServiceApplication",time=40)
Reference resources: python string formatting (format)
JavaScript
JavaScript and Node.js do not directly provide corresponding methods to complete the above requirements, but you can encapsulate the methods yourself.
//此代码来源自 https://github.com/component/pad/blob/master/index.js
/**
* Pad `str` to `len` with optional `c` char,
* favoring the left when unbalanced.
*
* @param {String} str
* @param {Number} len
* @param {String} c
* @return {String}
* @api public
*/
function pad(str, len, c) {
c = c || ' ';
str = str + "";
if (str.length >= len) return str;
len = len - str.length;
var left = Array(Math.ceil(len / 2) + 1).join(c);
var right = Array(Math.floor(len / 2) + 1).join(c);
return left + str + right;
}
/**
* Pad `str` left to `len` with optional `c` char.
*
* @param {String} str
* @param {Number} len
* @param {String} c
* @return {String}
* @api public
*/
function left(str, len, c){
c = c || ' ';
str = str + "";
if (str.length >= len) return str;
return Array(len - str.length + 1).join(c) + str;
}
/**
* Pad `str` right to `len` with optional `c` char.
*
* @param {String} str
* @param {Number} len
* @param {String} c
* @return {String}
* @api public
*/
function right(str, len, c){
c = c || ' ';
str = str + "";
if (str.length >= len) return str;
return str + Array(len - str.length + 1).join(c);
}
The demand plan is as follows:
data = {
level:"INFO",
pid:"10000",
thread:"main",
method:"AccountServiceApplication",
time:"40"
}
`${
left(data.level,5)} ${pad(data.pid,6)} --- [${
left(data.thread,15)}] ${
left(data.method,30)} 执行时长 ${
right(data.time,6)} ms.`
There are many powerful libraries in the JS camp that can meet the above needs, such as string-format