java使字符串的数字加一


/**

* 字符串+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();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM