加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Java字符串长度不够前面或后面补0 

(2015-08-20 10:21:30)
标签:

jarryliu

lsfhack

java字符串补0

分类: JAVA专区
1、 第一种字符串补0
public static String addZeroForNum(String str, int strLength) {
    int strLen = str.length();
    StringBuffer sb = null;
     while (strLen < strLength) {
           sb = new StringBuffer();
           sb.append("0").append(str);// 左补0
        // sb.append(str).append("0");//右补0
           str = sb.toString();
           strLen = str.length();
     }
    return str;
}

2、数字流水号长度不够补0方法
public static String codeAddOne(String code, int len){
   Integer intHao = Integer.parseInt(code);
   intHao++;
   String strHao = intHao.toString();
   while (strHao.length() < len) {
       strHao = "0" + strHao;
     }
   return strHao;
}

用java中的DecimalFormat,可以简化:

// 流水号加1后返回,流水号长度为15
private static final String STR_FORMAT = "000000000000"; 
public static String haoAddOne(String liuShuiHao){
    Integer intHao = Integer.parseInt(liuShuiHao);
    intHao++;
    DecimalFormat df = new DecimalFormat(STR_FORMAT);
    return df.format(intHao);
}

0

阅读 收藏 喜欢 打印举报/Report
前一篇:JSTL 获取参数
后一篇:net.sf.jsonobject
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有