字符串+1的方法原创
金蝶云社区-湖南客户成功_刘敏
湖南客户成功_刘敏
46人赞赏了该文章 1,375次浏览 未经作者许可,禁止转载编辑于2023年01月29日 10:03:42
/**
* 字符串+1方法,该方法将其结尾的整数+1,适用于任何以整数结尾的字符串,不限格式,不限分隔符。
* @author zxcvbnmzb
* @param testStr 要+1的字符串
* @return +1后的字符串
* @exception NumberFormatException
*/
public static String addOne(String testStr){
String[] strs = testStr.split("[^0-9]");//根据不是数字的字符拆分字符串
String numStr = strs[strs.length-1];//取出最后一组数字
if(numStr != null && numStr.length()>0){//如果最后一组没有数字(也就是不以数字结尾),抛NumberFormatException异常
int n = numStr.length();//取出字符串的长度
int num = Integer.parseInt(numStr)+1;//将该数字加一
String added = String.valueOf(num);
n = Math.min(n, added.length());
//拼接字符串
return testStr.subSequence(0, testStr.length()-n)+added;
}else{
throw new NumberFormatException();
}
}
}


赞 46