Java printff 字符串只输出格式 [英] Java printff string only output formatting
本文介绍了Java printff 字符串只输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 printf 格式化输出.
How do I format output by using printf.
假设我想在格式化程序中打印以下内容.
suppose i want to print following in formatter.
testing testing testing testing
testingggg testingggg testingggg testingggg
我不是问使用\t",我问的是 printf 样式格式?如果有人能给我建议和例子.或者可以与示例链接.我可以满足我的需要.
I am not asking about using "\t", I am asking on printf style formatting? If some one can give me suggestion and example. Or may be link with example. I can do it to fit my need.
推荐答案
我认为您正在寻找 Formatter 类,它为 Java 执行 printf 风格的格式化.
I think you're looking for the Formatter class, which does printf-style formatting for Java.
例如,应用于您的输入:
For example, applied to your input:
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
这篇关于Java printff 字符串只输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文