我试图将字符串定位在一条线上的某个起始位置上,无论先前的文本在哪里结束。例如:

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..

选项的解释从位置30开始,无论前面的文本如何。您如何使用java.util.formatter完成此操作?我已经用宽度进行了测试(%30s)和精度(%.30)参数,都不提供理想的结果。

有帮助吗?

解决方案

您可以采用“宽度”参数,这样:

    System.out.println(String.format("%-30s%s", "  -a, --all", "do not ignore entries starting with ."));
    System.out.println(String.format("%-30s%s", "  -A, --almost-all", "do not list implied . and .."));

编辑
如果您有兴趣,请完成概述。它根本不大,格式化功能非常有限。
http://java.sun.com/j2se/1.5.0/docs/api/java/java/util/formater.html#syntax

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top