There is the following requirement to generate a serial number that starts with D and is followed by a 6-digit sequence number, such as D000001, D000002, D000003... How to deal with it, use StringUtils.leftPad in commons-lang3 to achieve:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
package com.cjian;
import org.apache.commons.lang3.StringUtils;
/**
* @Author: cjian
* @Date: 2022/8/31 19:33
* @Des:
*/
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("D" + StringUtils.leftPad(i + "", 6, "0"));
}
}
}
D000000
D000001
D000002
D000003
D000004
D000005
D000006
D000007
D000008
D000009
Or, as some command-line returned messages require, spaces should be indented by entry:
package com.cjian;
import org.apache.commons.lang3.StringUtils;
/**
* @Author: cjian
* @Date: 2022/8/31 19:33
* @Des:
*/
public class Test {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
System.out.println(i + ")");
for (int j = 1; j < 5; j++) {
//为了效果展示,使用-代替空格
System.out.println(StringUtils.leftPad(j + ")", 4, "-"));
}
}
}
}
1)
--1)
--2)
--3)
--4)
2)
--1)
--2)
--3)
--4)
3)
--1)
--2)
--3)
--4)
4)
--1)
--2)
--3)
--4)