博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java使字符串的数字加一
阅读量:4680 次
发布时间:2019-06-09

本文共 708 字,大约阅读时间需要 2 分钟。

/**

* 字符串+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 = (n, added.length());
        
//拼接字符串
        
return 
testStr.subSequence(
0
, testStr.length()-n)+added;
    
}
else
{
        
throw 
new 
NumberFormatException();

转载于:https://www.cnblogs.com/muhy/p/10953671.html

你可能感兴趣的文章
[翻译]WPF控件库 MaterialDesignInXamlToolkit (1)
查看>>
hibernate validation HV000030: No validator could be found for constraint
查看>>
前端优化
查看>>
bzoj1511 [POI2006]OKR-Periods of Words kmp+乱搞
查看>>
心语4
查看>>
Telink MESH SDK 如何使用PWM
查看>>
LR SP PC
查看>>
C# 图片识别(支持21种语言)【转】
查看>>
C# 循环语句 for
查看>>
jQuery基础教程
查看>>
python class(1)
查看>>
模拟手工测试操作页面上的元素---留
查看>>
P2709 小B的询问
查看>>
九度OJ 1054:字符串内排序 (排序)
查看>>
第三组的抓包作业
查看>>
ILNumerics项目的应用之线性方程
查看>>
django考点
查看>>
python-socket
查看>>
.NET 分布式技术比较
查看>>
SpringMVC视频
查看>>