在java中格式化字符串

时间:2013-12-29 11:49:19

标签: java string

我想将状态消息打印到Console中。

   System.out.format("%-60s%10s\n", "Status 1", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2 and some text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2 and some very long text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2 and some very very very long text", "[Failed]");
            System.out.format("%-60s%10s\n", "Status 2", "[Success]");
            System.out.format("%-60s%10s\n", "Status 2", "[Failed]");

但正如您所看到的,第二列消息格式不正确。

Status 1                                                     [Success]
Status 2                                                      [Failed]
Status 2                                                      [Failed]
Status 2 and some text                                        [Failed]
Status 2                                                      [Failed]
Status 2                                                     [Success]
Status 2 and some very long text                              [Failed]
Status 2                                                     [Success]
Status 2 and some very very very long text                    [Failed]
Status 2                                                     [Success]
Status 2                                                      [Failed]

我想打印这样的消息:

Status 1                                                      [Success]
Status 2                                                      [Failed]
Status 2                                                      [Failed]
Status 2 and some text                                        [Failed]
Status 2                                                      [Failed]
Status 2                                                      [Success]
Status 2 and some very long text                              [Failed]
Status 2                                                      [Success]
Status 2 and some very very very long text                    [Failed]
Status 2                                                      [Success]
Status 2                                                      [Failed]

我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

为什么不添加"-"使最后一个字段左对齐,作为第一个字段?我想"%-60s%-10s\n"会起作用。

答案 1 :(得分:0)

您可以尝试此代码

  System.out.format("%-60s%.10s\n", "Status 1", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2 and some text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2 and some very long text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2 and some very very very long text", "[Failed]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Success]");
    System.out.format("%-60s%.10s\n", "Status 2", "[Failed]");