java里面使用string.format实现空格右填充代码如下:
package cn.com.songjy;
import java.text.NumberFormat;
public class NumberFormatTest {
public static void main(String[] args) {
int i = 1;
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
nf.setMaximumIntegerDigits(4);
nf.setMinimumIntegerDigits(4);
System.out.println(nf.format(i));
}
}
public class TestStringFormat {
public static void main(String[] args) {
int youNumber = 1;
String str = String.format("%04d", youNumber);
System.out.println(str); // 0001
}
}
private static final String STR_FORMAT = "0000";
public static String haoAddOne_2(String liuShuiHao){
Integer intHao = Integer.parseInt(liuShuiHao);
intHao++;
DecimalFormat df = new DecimalFormat(STR_FORMAT);
return df.format(intHao);
}