如何用点格式器保留整数?

问题描述

| 我知道我们可以用这样的格式化程序左移整数:
String.format(\"%7d\",234);   // \"    234\"
String.format(\"%07d\",234);  // \"0000234\"
String.format(\"%015d\",234); // \"0000000000000234\"
但是,如何用点代替零(例如纯文本内容索引)?
String.format(\"%.13d\",234); // doesn\'t work
我想产生这个:
..........234
我知道我可以使用循环来添加点,但是我想知道是否有一种方法可以使用格式化程序。     

解决方法

        我认为没有内置的
.
填充,但是您可以填充空格然后替换它们。
 String.format(\"%15d\",234).replaceAll(\' \',\'.\');
    ,        另一种方法是使用Apache Commons Lang lib。 http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringUtils.html#leftPad%28java.lang.String,%20int,%20char%29 grundprinzip已经指出...     ,        无法单独使用格式化程序,但是
String.format(\"%015d\",234).replaceFirst(\"0*\",\"\\.\");
应该做得很好。 (自然地,您必须对字符串进行某些操作-这个将产生一个String对象,然后该对象消失,因为它没有分配任何内容。) 更新资料 该死,忘记了正则表达式中的重复“ 6”。     ,        您可以手动执行。它不是很漂亮,但是可以用。
String str = Integer.toString( 234 );
str = \"...............\".substring( 0,Math.max( 0,15 - str.length() ) ) + str;