开发中经常会用到字符串的截取函数,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;
}