【发布时间】:2017-09-04 01:32:32
【问题描述】:
我正在尝试用空格将字符串填充到右侧,以使不同的字符串长度在同一点对齐。我尝试使用以下方法来做到这一点。但是,我不知道如何在 %s 中使用变量 namePad,就好像我们使用实数一样(例如使用 %(namePad)s 而不是 %5s)
public String toString() {
int namePad = 10-name.length();
return String.format(id + " -" + " %-5s" + "%10s" , name, "");
}
输出样本:
All customers:
1 - Bill Gates *// padding all the strings to here
2 - Trump *
3 - Tali *
4 - James Bond *
【问题讨论】:
标签: java string text-alignment