Java printff字符串只输出格式(Java printff string only output formatting)

编程入门 行业动态 更新时间:2024-10-25 15:33:47
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 testingggg

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风格的格式化。

例如,适用于您的输入:

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

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());

Produces:

testing testing testing testing testingggg testingggg testingggg testingggg

更多推荐

本文发布于:2023-08-02 11:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   格式   printff   Java   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!