java返回某个字符串第某次出现的位置

  • Post author:
  • Post category:java


    public static Integer indexOfStr(String str,String findStr, int index){
        Pattern pattern = Pattern.compile(findStr);
        Matcher findMatcher = pattern.matcher(str);
        int number = 0;
        while(findMatcher.find()) {
            number++;
            if(number == index){
                break;
            }
        }
        try {
            return findMatcher.start();
        } catch (Exception e) {
            return null;
        }
    }



版权声明:本文为ZuShang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。