Java printff 字符串只输出格式化

标签 java printf string-formatting output-formatting

如何使用 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    

关于Java printff 字符串只输出格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609249/

相关文章:

java - 使用 Lotus Notes 发送加密电子邮件

Java 中的 java.lang.reflect.InitationTargetException

c - system() 函数未按预期执行 shell 命令

python - f-string,多种格式说明符

formatting - 如何格式化标准 ML 打印输出?

java 字符串格式 : numbers with localization

java - 使用 pdfbox 创建新的自定义 COSBase 对象?

java - Windows 2012 Server 用户锁定时 Camel 停止消费

c - 为什么我的 scanf 输入不能正确打印?

c - 我想做一个打印反向的代码