如何使用制表器格式化输出以使表格看起来清晰

时间:2016-02-11 11:40:59

标签: java string postgresql formatting

我从postgresql获取输出。我想让它变得如此清晰。

例如:

 content    | tablename | columnname | rowctid 
------------+-----------+------------+---------
 blabla     | s3        | usename    | (0,11)
 public1    | s2        | relname    | (7,29)
 public 2   | w         | body       | (0,2)

我如何使用制表机等?我将从这开始,例如:

System.out.println(" content    | tablename | columnname | rowctid");
System.out.println("------------+-----------+------------+---------");
for (int i = 0, i<x, i++) {
System.out.println(con_var + "   |  " +tab_var + "   |  " +col_var + "   |  " +row_id);
}

但我认为制表师并不工作。另外,如果一个单词比另一个单词长,那么&#39; |&#39;会站在不同的位置。

1 个答案:

答案 0 :(得分:2)

试试这样:

    System.out.println(String.format("%30s", "content    |") 
            + String.format("%30s", "tablename |") 
            + String.format("%30s", "columnname |")
            + String.format("%30s", "rowctid |"));
    System.out.println(String.format("%30s", "+").replaceAll(" ", "-") 
            + String.format("%30s", "+").replaceAll(" ", "-")
            + String.format("%30s", "+").replaceAll(" ", "-") 
            + String.format("%30s", "+").replaceAll(" ", "-"));
    for (int i = 0, i<x, i++) {
        System.out.println(String.format("%30s", con_var + "   |  ") 
                +  String.format("%30s", tab_var + "   |  ") 
                +  String.format("%30s", col_var + "   |  ") 
                +  String.format("%30s", row_id + "   |  "));
    }

在此示例中,30是列大小。根据需要进行更改。

相关问题