Java程序将字符串格式化为表
要将字符串格式化为表格,请使用System.out.format。
对于我们的表,我们给了15s,这意味着15个空格。对于索引之后的第二列以相同的方式进行。
|%1$-15s|%2$-15s|
将上述内容添加到字符串中,这样就不必一次又一次地添加它。
以下是一个示例。
示例
public class Demo {
public static void main(String []args) {
String strFormat = "|%1$-15s|%2$-15s|\n";
System.out.format(strFormat, "One", "Two");
System.out.format(strFormat, "Three", "Four");
System.out.format(strFormat, "Five", "Six");
System.out.format(strFormat, "Seven", "Eight");
System.out.format(strFormat, "Nine", "Ten");
System.out.format(strFormat, "Eleven", "Twelve");
System.out.format(strFormat, "Thirteen", "Fourteen");
System.out.format(strFormat, "Fifteen", "Sixteen");
}
}输出结果
|One |Two | |Three |Four | |Five |Six | |Seven |Eight | |Nine |Ten | |Eleven |Twelve | |Thirteen |Fourteen | |Fifteen |Sixteen |
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话