假设我想以某种特定格式打印“Item”及其“price”
abc 2
asdf 4
qwer xyz 5
AND 2,4,5必须像一列一样.
为此我试过 –
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s%25s", "abc","2"));
sb.append(String.format("%s%25s", "asdf","4"));
sb.append(String.format("%s%25s", "qwer xyz","5"));
tv.setText(sb.toString()); //tv is a text view
但输出是 –
abc 2
asdf 4
qwer xyz 5
我想要“abc”,在25个空格之后,我想要“5”,但它从abc开始计算25个空格
解决方法:
你混淆了订单试试这个:
StringBuilder sb = new StringBuilder();
sb.append(String.format("%-25s%s", "abc","2"));
sb.append(String.format("%-25s%s", "asdf","4"));
sb.append(String.format("%-25s%s", "qwer xyz","5"));
tv.setText(sb.toString()); //tv is a text view
25之前的减号意味着反转文本的对齐方式.最后一步是使用等宽字体.您可以使用以下行来实现:
tv.setTypeface(Typeface.MONOSPACE);
标签:java,string-formatting
来源: https://codeday.me/bug/20190529/1175075.html