Java 初学者 - 字符串格式 = " | %-""maxW[j]""s"; - 这个字符串有什么作用?

发布于 2024-10-08 23:02:01 字数 312 浏览 0 评论 0 原文

我是Java菜鸟,我遇到了下面的代码,但无法弄清楚它的功能。 maxw[] 是一个 int 类型的数组。 row[] 是 String 类型的数组。

             String format = " | %-"+maxW[i]+"s";
       System.out.printf(format,row[i]);

我的问题是: 对于此语句: System.out.printf(format,row[i]);只有“|”在 row[i] 的值被打印之后,为什么 '%- (maxW[i] 的值) 和 's' 没有被打印?

I am a noob in Java and I came across the code below and couldn't figure out its function.
maxw[] is an array of type int.
row[] is an array of type String.

             String format = " | %-"+maxW[i]+"s";
       System.out.printf(format,row[i]);

My questions is :
For this statement : System.out.printf(format,row[i]); only '|' and after that value of row[i] are getting printed so why are '%- (value of maxW[i]) and 's' not getting printed ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一个人练习一个人 2024-10-15 23:02:01

此处描述了 Java 格式说明符。

此代码动态创建一个格式说明符,它将左对齐字符串 (row[i]),其最小宽度由 maxW[i] 指定。

格式说明符遵循下面第一行的模式。在下面,我对齐了上面的代码,显示了它适合模式的位置。

    %[argument_index$][flags] [width] [.precision]conversion
" | %                  -     "+maxW[i]           +"s";

Java format specifiers are described here.

This code is dynamically creating a format specifier that will left-justify a string (row[i]) with a minimum width specified by maxW[i].

Format specifiers follow the pattern on the first line below. Underneath I've aligned the code above, showing where it fits into the pattern.

    %[argument_index$][flags] [width] [.precision]conversion
" | %                  -     "+maxW[i]           +"s";
記憶穿過時間隧道 2024-10-15 23:02:01

format 方法假设 String 描述了文档中所述的格式。它以特殊方式处理 %x 等,并将其替换为第一个、第二个等参数。

如果您想了解更多信息,我建议您阅读此方法的 Javadoc。

The format method assumes the String decribes a format as it states in the documentation. It handles %x or the like a special way replacing it with the first, second etc argument.

If you want to know more I suggest you read the Javadoc for this method.

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