1

我试图将一个字符串定位在一行的某个起始位置,而不管前面的文本在哪里结束。例如:

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) 参数进行了测试,但都没有提供所需的结果。

4

1 回答 1

2

您可以使用“宽度”参数,如下所示:

    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/util/Formatter.html#syntax

于 2010-06-07T22:18:36.017 回答