字符串和数字结合,提取数字,操作数字

最近写了一个算法,是字符串包含数字,需要数字结尾的,要数字递加,废话不多,上代码

/**
     * 名称与数字调度算法
     * @param name
     * @return
     */
    private String pointNameScheduling(String name) {
        boolean b = Pattern.matches("\\d+", name);// 判断是不是数字
        if (b) {
            int result = Integer.parseInt(name);
            result = result + 1;
            return Integer.toString(result);
        } else {
            Matcher m = Pattern.compile("[^0-9]").matcher(name);
            String mind = m.replaceAll("").trim();
            if (mind.equals("")) {
                return name + 1;
            }
            int result = Integer.parseInt(mind);
            if (name.endsWith(mind)) {
                result = result + 1;
                return name.replace(mind, String.valueOf(result));
            } else {
                String circle = "";
                for (int i = 1; i < mind.length(); i++) {
                    if (name.endsWith(mind.substring(i, mind.length()))) {
                        circle = mind.substring(i, mind.length());
                        result = Integer.parseInt(circle);
                        result = result + 1;
                        break;
                    }
                }
                if (circle.equals("")) {
                    return name + 1;
                } else {
                    return name.replace(circle, String.valueOf(result));
                }
            }
        }

    }

就是可以把字符串中的数字提取出来,如果是以数字结尾的,可以递加或者其他操作,同时,提取的数字中,非结尾数字则去除掉,相反的,也可以简单修改变成字符串开头的数字操作,满足相关需求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值