2013-07-22 64 views
2

我們可以使用空格填充輸出以適應java中使用printf的「固定寬度列」。例如:帶有重複週期的java printf

System.out.printf("%-30s - %s%n", "hello hello", "goodbye goodbye"); 
System.out.printf("%-30s - %s%n", "lots of cats", 
     "large amounts of dogs"); 
System.out.printf("%-30s - %s%n", "a crowd of teachers", "no students"); 
System.out.printf("%-30s - %s%n", "three desks", "twenty boxes"); 

給出輸出

hello hello     - goodbye goodbye 
lots of cats     - large amounts of dogs 
a crowd of teachers   - no students 
three desks     - twenty boxes 

我想實現的是:

hello hello. . . . . . . . . . . goodbye goodbye 
lots of cats . . . . . . . . . . large amounts of dogs 
a crowd of teachers. . . . . . . no students 
three desks. . . . . . . . . . . twenty boxes 

hello hello..................... goodbye goodbye 
lots of cats.................... large amounts of dogs 
a crowd of teachers............. no students 
three desks..................... twenty boxes 

回答

2

貌似這個Change default padding character in java-printf之前已經嘗試。建議一種解決方法,但它可能適合您的方案,但您也可以修改java源代碼,但我認爲每個人都會強烈建議您不要這樣做。

+0

夠公平的..所以答案是否定的,否則不能寫我自己的代碼。好吧。 –

+0

但是,請正確使用它,因爲您的字裏面有空格 – ObieMD5