使用String.format将不同大小的对象放置在特定位置

发布于 2024-09-08 15:11:01 字数 713 浏览 2 评论 0原文

我有一堆数据需要美化和输出。 当前使用的代码是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2024-09-15 15:11:01

在 Eclipse 中尝试了这段代码:

public class StringFormatting
{
    public static void main(String[] args) 
    {         
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-30 21:00:00", 9, 33, 599));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 15, 6, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
    }
}

我得到了正确对齐的格式化输出:

2006-09-29 00:01:01     36               19               600

2006-09-30 21:00:00      9               33               599

2006-09-29 00:01:01     15                6               600

2006-09-29 00:01:01     36               19               600

也许 Eclipse 控制台窗口在这里做了正确的工作。打败我了...

Tried this code inside Eclipse:

public class StringFormatting
{
    public static void main(String[] args) 
    {         
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-30 21:00:00", 9, 33, 599));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 15, 6, 600));
         System.out.println(String.format("%1s" + "%7d" + "%17d" + "%18d" + "\n", "2006-09-29 00:01:01", 36, 19, 600));
    }
}

I got the correct aligned formatted output:

2006-09-29 00:01:01     36               19               600

2006-09-30 21:00:00      9               33               599

2006-09-29 00:01:01     15                6               600

2006-09-29 00:01:01     36               19               600

Maybe it's Eclipse Console Window that is doing the right job here. Beats me...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文