獲取母字串中某個子字串的某個確定的index值

DongLxu發表於2021-01-05

該方法的作用,旨在:在longStr中找str,根據所傳的theIndex來返回str在longStr中的第幾個索引的字串位置:

private int getIndexOfStrInLongStr(String longStr,String Str,int theIndex){
    Set<Integer> indexInt = new LinkedHashSet<>();
    int indexStr = 0;
    int index = 0;
    if(StringUtils.isNotEmpty(longStr)){
        while (index != longStr.length()){
                indexStr = longStr.indexOf(Str,index);
                indexInt.add(indexStr);
            if(indexInt.size() < theIndex){
                index++;
            }else {
                break;
            }
        }
    }else {
        logger.error("本次需要獲取的母字串為空!");
    }

     return indexStr;
}

相關文章