带有重复句点的java printf

标签 java printf string-formatting

我们可以使用 printf 在 java 中使用空格来填充输出以适应“固定宽度列”。示例:

System.out.printf("%-30s - %s%n", "hello hello", "goodbye goodbye");
System.out.printf("%-30s - %s%n", "lots of cats",
        "large amounts of dogs");
System.out.printf("%-30s - %s%n", "a crowd of teachers", "no students");
System.out.printf("%-30s - %s%n", "three desks", "twenty boxes");

给出输出

hello hello                    - goodbye goodbye
lots of cats                   - large amounts of dogs
a crowd of teachers            - no students
three desks                    - twenty boxes

我想要实现的是:

hello hello. . . . . . . . . . . goodbye goodbye
lots of cats . . . . . . . . . . large amounts of dogs
a crowd of teachers. . . . . . . no students
three desks. . . . . . . . . . . twenty boxes

hello hello..................... goodbye goodbye
lots of cats.................... large amounts of dogs
a crowd of teachers............. no students
three desks..................... twenty boxes

最佳答案

看起来之前已经尝试过 Change default padding character in java-printf 。建议使用一种解决方法,但它可能适合您的场景,尽管您也可以修改 java 源代码,但我认为每个人都会强烈建议不要这样做。

关于带有重复句点的java printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17778745/

相关文章:

java - 我如何发现永久代中的内容

java - 我不明白为什么这个递归不起作用

Java Web 应用程序似乎在 Tomcat 中快速闲置

c - printf 在程序退出时刷新

c - 为什么C语言中字符串转十六进制需要用0xff来完成?

C - fprintf() & printf() 删除数组元素内存

excel - 使用公式格式化 Excel 单元格中的文本子集

java - 无法连接到 Red Hat 上的 H2 文件数据库

python - 如何使用 Python 将点放入数字中?

Bash %s 格式说明符在打印输出时消除了字符串中的空格