获取指定字符第几次出现的下标

public static int getIndexOf(String data, String str,int num) {
     Pattern pattern = Pattern.compile(str);
     Matcher findMatcher = pattern.matcher(data);
     //标记遍历字符串的位置
     int indexNum = 0;
     while (findMatcher.find()) {
         indexNum++;
         if (indexNum == num) {
             break;
         }
     }
     return findMatcher.start();
 }
posted @ 2023-05-11 16:27  LIUAOJUN  阅读(56)  评论(0)    收藏  举报