最近写了一个算法,是字符串包含数字,需要数字结尾的,要数字递加,废话不多,上代码
/**
* 名称与数字调度算法
* @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));
}
}
}
}
就是可以把字符串中的数字提取出来,如果是以数字结尾的,可以递加或者其他操作,同时,提取的数字中,非结尾数字则去除掉,相反的,也可以简单修改变成字符串开头的数字操作,满足相关需求