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文章有一些关于格式的信息,但没有任何关于中心的理由。
输出实际上是一个表,所以某些原因问题没有正确显示表。 – rana
请参阅'StringUtils' [http://stackoverflow.com/questions/2709993/how-to-center-string-output-using-printf-and-variable-width-java](http://stackoverflow.com/questions/2709993/how-to-center-string-output-using-printf-and-variable-width-java)**或** [http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusingthe spacecharacter .htm](http://www.java2s.com/Code/Java/Data-Type/CentersaStringinalargerStringofsizesizeusingspacecharacter.htm) – ecle
@eee是标准库中的StringUtils? – rana