//子字符串modelStr在字符串str中第count次出现时的下标
private int getFromIndex(String str, String modelStr, Integer count) {
//对子字符串进行匹配
Matcher slashMatcher = Pattern.compile(modelStr).matcher(str);
int index = 0;
//matcher.find();尝试查找与该模式匹配的输入序列的下一个子序列
while(slashMatcher.find()) {
index++;
//当modelStr字符第count次出现的位置
if(index == count){
break;
}
}
//matcher.start();返回以前匹配的初始索引。
return slashMatcher.start();
}
java获取某个子字符串在整个字符串中第N次出现时的下标索引
最新推荐文章于 2024-06-19 23:12:25 发布