java拼接字符,数据库字符和数字拼接的字符串作为字段,如ID,主键

基本思想就是从数据库取值,然后String和Integer相互转换,截取字符串,拼接的过程。在此记录,分享。

此处核心为字符的处理,不包括数据库取值,向前端传值代码,main里的代码仅供初学者参考,大虾请无视,哈哈。另外String,StringBuffer,Integer的API真的很强大。

public static void main(String [] args){

String hbid = "HB000010";//假装是从数据库中拿到的数据

System.out.println(hbid.charAt(1));//指定下标的字符串
System.out.println("0最后一次出现的位置"+hbid.lastIndexOf("0"));
System.out.println("字符串拼接方法:"+hbid.concat("AB"));
//1、第一步:截取字符串
String substr = hbid.substring(hbid.lastIndexOf("B")+1);
System.out.println("截取后的字符串:"+substr);
//2、第二步:将HB后面的字符串转换成Integer类型
Integer i = Integer.parseInt(substr);
//3、第三步:给值加1
System.out.println("算法执行后的结果:"+i++);//
//4、将计算结果转回字符串
String result = Integer.toString(i);
//5、计算字符串长度
int length = result.length();
System.out.println(length);
//6、第六步:将结果字符串前面补足HB,数字用“0”补足6位
StringBuffer sbf = new StringBuffer("HB");
String newnmb = StringUtils.leftPad(result, 6,'0');
System.out.println(newnmb);//补足后的结果
System.out.println("最终的结果"+sbf.append(newnmb));


}

猜你喜欢

转载自blog.csdn.net/Eric_YS/article/details/78124168