使用String.format将不同大小的对象放置在特定位置
我有一堆数据需要美化和输出。 当前使用的代码是:
String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", string, int, int, int);
第一个文件的结果输出是:
2006-09-16 09:00:01 1 0 501
2006-09-16 08:15:00 0 4 401
2006-09-15 07:05:15 0 3 301
字符串始终是该长度,但问题出在接下来的两个整数上,因为它们的长度可以是 1 或 2。
我 然后我得到其他文件:
2006-09-29 00:01:01 36 19 600
2006-09-30 21:00:00 9 33 599
2006-09-28 05:16:00 15 6 599
第二两行向左移动 1 个空格。是否可以使无论整数的长度如何,它们总是出现在同一位置,从而消除发生的移位因子?
I have a bunch of data that I need to beautify and output. The current code I'm using is:
String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", string, int, int, int);
The resulting output from my first file is :
2006-09-16 09:00:01 1 0 501
2006-09-16 08:15:00 0 4 401
2006-09-15 07:05:15 0 3 301
The string is always that length, but the problem comes with the next two ints, because they can be of either length 1 or 2.
When I use my other file then I get :
2006-09-29 00:01:01 36 19 600
2006-09-30 21:00:00 9 33 599
2006-09-28 05:16:00 15 6 599
The second two lines are shifted to the left by 1 space. Is it possible to make it so that regardless of the length of the ints, they always appear in the same place removing the shift factor that occurs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Eclipse 中尝试了这段代码:
我得到了正确对齐的格式化输出:
也许 Eclipse 控制台窗口在这里做了正确的工作。打败我了...
Tried this code inside Eclipse:
I got the correct aligned formatted output:
Maybe it's Eclipse Console Window that is doing the right job here. Beats me...