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

标签 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);
}

但我认为制表器不起作用。此外,如果一个单词比另一个单词长,则“|”会站在不同的位置。

最佳答案

尝试这样:

    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 是列大小。根据需要进行更改。

关于java - 如何使用制表符格式化输出以使表格看起来清晰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35338390/

相关文章:

string - XSLT 中的整数到字符串转换

php - PGSQL 未在 IIS 下的 PHP 5.3 Windows 中加载

java - 如果发生IOException,这个InputStream会被正确关闭吗?

Java - 二进制插入排序 StackOverFlowError

java - Java中的静态变量

java - 无法上传 aws 设备场中的项目

string - 如何在 Rails 应用程序中处理 "ellipsify"冗长的文本?

python - 比较两个字符串,包括重复的字母?

postgresql - postgres 从 regexp_split_to_table 生成的行中选择并抛出错误的位置

database - 如何在 Sequelize 'where' 中连接关系?