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