1:用0补齐字符串位数
public static String addZeroForNum(String str, int strLength,boolean position) {
int strLen = str.length();
while (strLen < strLength) {
StringBuffer sb = new StringBuffer(str);
if (position){
sb.append(ZERO);
}else{
sb.insert(0,ZERO);
}
str = sb.toString();
strLen = str.length();
}
return str;
}
2:用指定字符补齐字符串位数
public static String addValueForNum(String str,String value, int strLength,boolean position) {
int strLen = str.length();
if (StringUtils.isBlank(value)){
value = ZERO;
}
while (strLen < strLength) {
StringBuffer sb = new StringBuffer(str);
if (position){
sb.append(value);
}else{
sb.insert(0,value);
}
str = sb.toString();
strLen = str.length();
}
return str;
}