使用String.format Java以正确的方式对齐所有字符串

时间:2019-09-28 21:30:22

标签: java string-formatting stringbuilder

我通过在StringBuilder中附加所有字符串,然后将其转储到文件中来创建文件。但是当我检查文件时(很明显),我在每一行中的所有字符串都没有正确对齐。

下面是上层通过传递所有必要内容而调用的代码。

  private static StringBuilder appendStrings(String pro, List<ProcConfig> list,
      StringBuilder sb) {
    for (ProcConfig pc : list) {
      sb.append(pro).append(" ").append(pc.getNo()).append(" ").append(pc.getId())
          .append(" ").append(pc.getAddress()).append(" ").append(pc.getPort()).append(" ")
          .append(pc.getNumPorts()).append(" ").append(pc.getName())
          .append(System.getProperty("line.separator"));
    }
    sb.append(System.getProperty("line.separator"));
    return sb;
  }

这是上面代码中示例行的外观。每行之后都有一个新集,因此我想在每行之前将每组中的所有行正确对齐。

config 51 106 10.178.151.25 8095 5 tyt_87612nsas_woqa_7y2_0
config 51 104 10.124.192.124 8080 5 tyt_abc_pz1_rn03c-7vb_01

if_hello_abc_tree tyt.* then process_is_not_necessary 32 80 10.86.25.29 9091 5 tyt_goldenuserappslc22
if_hello_abc_tree tyt.* then process_is_not_necessary 51 50 10.174.192.209 9091 5 tyt_goldenuserapprno01

if_hello_abc_tree tyt.* then config 4 140 10.914.198.26 10001 1 silos_lvskafka-1702600
if_hello_abc_tree tyt.* then config 4 184 10.444.289.138 10001 1 silos_lvskafka-1887568

有什么办法可以使上述每一行正确对齐吗?所以输出应该是这样的:

config 51 106 10.178.151.25  8095 5 tyt_87612nsas_woqa_7y2_0
config 51 104 10.124.192.124 8080 5 tyt_abc_pz1_rn03c-7vb_01

if_hello_abc_tree tyt.* then process_is_not_necessary 32 80 10.86.25.29    9091 5 tyt_goldenuserappslc22
if_hello_abc_tree tyt.* then process_is_not_necessary 51 50 10.174.192.209 9091 5 tyt_goldenuserapprno01

if_hello_abc_tree tyt.* then config 4 140 10.914.198.26  10001 1 silos_lvskafka-1702600
if_hello_abc_tree tyt.* then config 4 184 10.444.289.138 10001 1 silos_lvskafka-1887568

更新

下面是现在如何生成它。正如您在“ IP地址”上看到的那样,如果将这两行进行比较,则略有偏离。

config 51 106   97.143.765.65 8095 5 abc_tyewaz1_rna03c-7nhl_02 
config 51 104  97.143.162.184 8080 5 abc_tyewaz1_rna03c-7vjb_01 

相反,我们可以生成如下内容吗?基本上使各列笔直。这可能吗?

config 51 106  97.143.765.65  8095 5 abc_tyewaz1_rna03c-7nhl_02 
config 51 104  97.143.162.184 8080 5 abc_tyewaz1_rna03c-7vjb_01 

1 个答案:

答案 0 :(得分:1)

首先:我不是Java开发人员,但是我对此有所了解。我知道这不是解决问题的最佳方法,但是您会明白这一点的。

代替多次调用append方法,请使用Formatter并像这样遍历它:

private static StringBuilder appendStrings(String pro, List<ProcConfig> list, StringBuilder sb) {
    Formatter formatter = new Formatter(sb);
    String template ="";
    for (ProcConfig pc : list) {
        if (pro.length() == 6)
            template = "%-6s %d %3d %-15s %d %d %s %n";
        else if (pro.length() > 35)
            template = "%-53s %d %3d %-15s %d %d %s %n";
        else
            template = "%-35s %d %3d %-15s %d %d %s %n";
        formatter.format(template, pro, pc.getNo(), pc.getId(), pc.getAddress(), pc.getPort(), pc.getNumPorts(), pc.getName());
    }
    formatter.close();
    return sb;
}

由于专业人士的长度不同,您可以将模板更改为适合每个专业人士的模板。

注意:不要忘记导入 java.util.Formatter

您可以使用格式说明符来指定数据的格式化方式。这是常见格式化程序的列表:

%S or %s: Specifies String
%X or %x: Specifies hexadecimal integer
%o: Specifies Octal integer
%d: Specifies Decimal integer
%c: Specifies character
%T or %t: Specifies Time and date
%n: Inserts newline character
%B or %b: Specifies Boolean
%A or %a: Specifies floating point hexadecimal
%f: Specifies Decimal floating point

正如我之前在评论中所说的,破折号表示第一个字符串是对齐的,并且每个字符串都会在模板中定义的字符数之后追加,因此请尝试查找适合您需求的最佳数字。

要了解有关格式化程序的更多信息,请查看herehere

相关问题