Java printff字符串只输出格式(Java printff string only output formatting)
如何使用printf格式化输出。
假设我想在格式化程序中打印以下内容。
testing testing testing testing testingggg testingggg testingggg testingggg我不是在问使用“\ t”,我问的是printf样式格式? 如果有人能给我一些建议和例子。 或者可以与示例链接。 我可以做到符合我的需求。
How do I format output by using printf.
suppose i want to print following in formatter.
testing testing testing testing testingggg testingggg testingggg testinggggI 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风格的格式化。
例如,适用于您的输入:
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 testinggggI 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());Produces:
testing testing testing testing testingggg testingggg testingggg testingggg更多推荐
发布评论