在 Java 中左填充字符串
要左填充字符串,可使用 String.format 并设置空格。
String.format("|%20s|", "demotext")如果在上面加上 30,它会从开头起将第一个字符串显示在 30 个空格之后。
String.format("|%30s|", "demotext")示例
public class Demo {
public static void main(String []args) {
System.out.print(String.format("|%20s|", "demotext"));
System.out.println("Left padded!");
}
}输出
| demotext|Left padded
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP