public class Divers {
public static void main(String args[]){
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
System.out.format(format, "Real", "", "Gagnon");
System.out.format(format, "John", "D", "Doe");
String ex[] = { "John", "F.", "Kennedy" };
System.out.format(String.format(format, (Object[])ex));
}
}
输出:
|FirstName |Init. |LastName |
|Real | |Gagnon |
|John |D |Doe |
|John |F. |Kennedy |
我希望输出居中。如果我不使用“-”标志,输出将右对齐。
我没有在 API 中找到使文本居中的标志。
This article有一些关于格式的信息,但没有关于center justify的信息。
原文由 rana 发布,翻译遵循 CC BY-SA 4.0 许可协议
我很快就解决了这个问题。您现在可以在
StringUtils.center(String s, int size)
String.format
。