【问题标题】:Align string in a column in a java在java中的列中对齐字符串
【发布时间】:2015-10-10 04:19:32
【问题描述】:

我一直在尝试将字符串对齐在一列中,但找不到解决方案。 这就是我得到的 - image

您可以看到字符串是如何不对齐的。有人可以帮我对齐它们。这就是我正在使用的。

String padded = String.format("%-12s       %-12s       %-12s       %-12s       %-12s", currentBalancepadded, radioButtonSelectedPadded, transactionAmountPadded, newBalancePadded, "X");

【问题讨论】:

  • 你也在使用固定宽度的字体吗?
  • 为什么不把它们放在一张桌子上?

标签: java string alignment


【解决方案1】:

您应该尝试连接您的字符串。要添加新行,您应该将 /n 字符连接到要在新行上的字符串之前。

例如:

public class Test 
{
public static void main (String[]  args)
   {
    String String1 = "";
    String String2 = "";
    String String3 = "";
    System.out.print(String1 + "/n" + String2 + "/n" + String3);
   }
}

【讨论】:

  • 这对于问题所在的缩进没有任何作用。
【解决方案2】:

您正在对所有字符串使用左对齐。尝试对表示金额的字符串使用右对齐。这将正确对齐小数位。

String padded = String.format("%12s %-12s %12s %12s %-12s", currentBalancepadded, radioButtonSelectedPadded, transactionAmountPadded, newBalancePadded, "X");

您可能需要调整“12”以与您的标题完全对齐。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多