java获取某个子字符串在整个字符串中第N次出现时的下标索引

来源:互联网 发布:皇马今晚比赛网络直播 编辑:程序博客网 时间:2024/06/14 00:14
//子字符串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();}

1 0
原创粉丝点击