Java printff字符串只输出格式

如何使用printf格式化输出。

假设我想在格式化程序中打印以下内容。

testing testing testing testing testingggg testingggg testingggg testingggg 

我不问有关使用“\ t”,我问的是printf样式格式? 如果有人可以给我建议和例子。 或者可以与示例链接。 我可以做到符合我的需要。

我想你正在寻找Formatter类,它为Java执行printf样式的格式化。

例如,应用于您的输入:

  StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb, Locale.US); formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s\n", "testing", "testing", "testing", "testing"); formatter.format("%1$-15s %2$-15s %3$-15s %4$-15s", "testingggg", "testingggg", "testingggg", "testingggg"); System.out.println(sb.toString()); 

生产:

 testing testing testing testing testingggg testingggg testingggg testingggg 

您可以使用System.out.printf("%sggg" , t)

其中t = "testing"并且ggg只是附加到此字符串上。

希望这会有所帮助:D