简体   繁体   English

Java-在输入时更改system.out.format的字符串标志

[英]Java - make string flag for system.out.format change on input

I'm trying to set the flag of the 我正在尝试设置标志

System.out.format("%s", string);

so the s has variable allocated size associated with it. 因此s具有与其相关的可变分配大小。

For example: I want 例如:我要

System.out.format("%MyVars", string);

Where MyVar would be a variable dependent on the size of a particular list in the program MyVar是一个变量,取决于程序中特定列表的大小

Where as of now I can only have static numbers like: 截至目前,我只能拥有静态数字,例如:

System.out.format("%5s", string);

How would I do this? 我该怎么做?

Why don't you use string concatenation to achieve your desired format? 为什么不使用字符串连接来实现所需的格式?

Below code snippet my help:- 下面的代码片段是我的帮助:-

int num = 5;
System.out.format("%" + num + "." + string.length() + "S", string);

You can do this using the below snippet: 您可以使用以下代码段执行此操作:

String string = "abc";
int size =5;
System.out.format("%d%s", size, string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM