简体   繁体   English

Java中的字符串格式,单词的最大宽度和右括号

[英]String Formatting in java with max width and closing bracket at the end of word

I want to get the below format 我想获得以下格式

abc 12 [hello-wo]       this is message1
abc 12 [hello-world]    this is message2
abc 10 [hello-wor]      this is message2  

I tried using 我尝试使用

String format1 = "%s %d [%s-%s] %s\n";
String format2 = "%s %d [%s-%-20s] %s\n";
    String a = String.format(format1,"abc","12",hello,"wo","this is message1");
    System.out.print(a);

I resulted like below by using two formats (format1 and format2) 通过使用两种格式(格式1和格式2),我得到如下结果

format1 格式1

abc 12 [hello-wda] this is message1
abc 12 [hello-world] this is message2
abc 10 [hello-wor] this is message2

format2 格式2

abc 12 [hello-wo                  ] this is message1
abc 12 [hello-world               ] this is message2
abc 12 [hello-wor                 ] this is message1

What I want is to align the last part of the parameter in same vertical position and closing square brackets should be at the end of the 4th parameter without any spaces. 我想要的是将参数的最后部分对齐在相同的垂直位置,并且将方括号放在第4个参数的末尾,且没有任何空格。

What you describe is transforming the argument. 您描述的是改变论点。 This is not what formatters generally do (unless with numbers and dates, ie pretty specific stuff). 格式化程序通常不会这样做(除非有数字和日期,即非常具体的东西)。

There is no way you can do what you want, I think, but you can work around it by using pattern and converting your args manually, like this: 我认为您无法做您想做的事情,但是您可以通过使用pattern并手动转换args来解决此问题,如下所示:

The pattern: 模式:

String PATTERN = "%s %d [%s-%-21s %s\n"; // notice that I've removed the closing bracket.
                                         // See below

Applying the arguments: 应用参数:

String arg = "world";
arg = arg.trim();
arg = arg.substring(0, Math.min(arg.length(), 20));
String.format(PATTERN, "abc", "12", "hello",
    /*Attention here: changing next argument by appending close bracket */
    arg + "]",
    "this is the message");

Here a trick how you can solve your problem : 这里有个技巧,可以解决您的问题:

public void formatString(String hello) {
    String format1 = "%s %s %s\n";
    String firstPart = String.format("%s %d [%s-%s]", "abc", 12, hello, "wo");
    String a = String.format(
            format1, firstPart, 
            String.format("%0" + Math.abs(30 - firstPart.length()) + "d", 0).replace("0", " "), 
            "this is message1");
    System.out.print(a);
}

Test case : 测试用例 :

formatString("hello-wda");
formatString("hello-world");
formatString("hello-wor");

Outputs 产出

abc 12 [hello-wda-wo]           this is message1
abc 12 [hello-world-wo]         this is message1
abc 12 [hello-wor-wo]           this is message1

The trick is to use two format, the first one to create the first part, we need this part to calculate the number of space needs to be equilibrium, the second format to combine the first part + the spaces between the first and second part + second part. 诀窍是使用两种格式,第一种创建第一部分,我们需要这一部分来计算需要均衡的空间数量,第二种格式将第一部分+第一部分和第二部分之间的空格+第二部分。

I used 30 as default value you can change it with your needs. 我使用30作为默认值,您可以根据需要进行更改。

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

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