JAVA 字符串 和 JS 字符串截取函数

    开发中经常会用到字符串的截取函数,JAVA 和JS 有各自的截取函数,使用时作个记录,以便区分和方便使用。

java 字符串
str.substring(begint,endint) 开始begint从0开始,结束 endint,长度 endint - begint,不包括endint   
//查找上一个记账区间,如201909则返回201908,如201901则返回201812
  public String preperiod(String period){
        String year,month,preperiod,preyear,premonth;
        Integer ipreyear,ipremonth;        
        
        year = period.substring(0,4);
          month = period.substring(4,6);
          if  (!month.equals("01"))
          { preyear = year; 
            ipremonth = Integer.parseInt(month) - 1;
            premonth = Integer.toString(ipremonth);
            if (premonth.length() == 1) {premonth = "0" + premonth;}
            preperiod = preyear + premonth;}
          else { ipreyear = Integer.parseInt(year) - 1;preyear  = Integer.toString(ipreyear);
                 premonth = "12";
                 preperiod = preyear + premonth;}
        return preperiod;
    }

//生成下一个凭证号
public String getnextvoucherno(){
        int intvoucherno;
        String newvoucherno = AccountRepository.getMaxvoucherno();
        
        if (newvoucherno == null) { newvoucherno = "800000";}
        else { intvoucherno = Integer.parseInt(newvoucherno) + 1; newvoucherno = Integer.toString(intvoucherno);}
                
        return newvoucherno;
        }      

js 字符串

stringObject.substr(start,length) substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
//生成下一个凭证号,后3位流水号处理,然后与前11位拼接生成新流水号
function billcodenext(billcode0){     
     var   billcode0s = billcode0.toString();
     var   billcode0s3 = billcode0s.substr(11,3);
    
     var   billcode0s3i = parseInt(billcode0s3) + 1;
     var   billcode0s3is = billcode0s3i.toString();
    
     if (billcode0s3is.length ==1) {billcode10 = "00" + billcode0s3is;}
     if (billcode0s3is.length ==2) {billcode10 = "0" + billcode0s3is;}
    
     return   billcode0s.substr(0,11) + billcode10;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值