右边有零填充

Cai*_*tos 23 java padding string-formatting

对不起,如果这个问题已经提出,我已经深入搜索,没有任何内容.

现在,我知道:

String.format("%05d", price);
Run Code Online (Sandbox Code Playgroud)

将左边的零填充我的价格,因此25的价格将导致00025

如果我想将它们填充到右边怎么办,结果是25000?如何 使用String.format模式执行此操作?

Rei*_*eus 41

你可以使用:

String.format("%-5s", price ).replace(' ', '0')
Run Code Online (Sandbox Code Playgroud)

我只能使用格式模式吗?

String.format使用Formatter.justify就像String.printf方法一样.从这篇文章中你会看到输出空间是硬编码的,所以使用它String.replace是必要的.


Pra*_*d D 5

试试这个 :

String RightPaddedString = org.apache.commons.lang.StringUtils.rightPad(InputString,NewStringlength,'paddingChar');
Run Code Online (Sandbox Code Playgroud)