使用String.format时如何缩进新行?

时间:2018-07-25 18:53:29

标签: java string formatting

我目前正在研究一个项目,并且正在使用基于文本的日志记录系统。我以“测试字符串\ n测试字符串2 \ n测试字符串3”的形式给我的方法字符串。

这是字符串格式的样子。

adminWriter.newLine();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String currDateTime = dateFormat.format(date);

adminWriter.write(String.format("%-20s | %-50s", currDateTime, text));

然后这样调用该方法...

Start.scribe.adminToDoWrite("User: "+this.riderID
                                    +"\nTo Delete Horse: "+this.horses[row].getHorseID()
                                    +"\nHorse Name: "+this.horses[row].getHorseName());

当前输出如下所示

{Date And Time} | User: (Number)
To Delete Horse: (Number)
Horse Name: (Name)

有没有让它看起来像这样的东西?这样每一行都自动缩进到String.format规则中?

{Date And Time} | User: (Number)
                  To Delete Horse: (Number)
                  Horse Name: (Name)

非常感谢

1 个答案:

答案 0 :(得分:1)

您需要更改

发件人:

Start.scribe.adminToDoWrite("User: "+this.riderID
                                +"\nTo Delete Horse: "+this.horses[row].getHorseID()
                                +"\nHorse Name: "+this.horses[row].getHorseName());

收件人:

Start.scribe.adminToDoWrite("User: "+this.riderID
            +"\n                       To Delete Horse: "+this.horses[row].getHorseID()
            +"\n                       Horse Name: "+this.horses[row].getHorseName());