Occurrences of substring in a string

后端 未结 24 2342
眼角桃花
眼角桃花 2020-11-22 03:35

Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)

String str = \"helloslkhe         


        
24条回答
  •  暖寄归人
    2020-11-22 03:52

    If you need the index of each substring within the original string, you can do something with indexOf like this:

     private static List getAllIndexesOfSubstringInString(String fullString, String substring) {
        int pointIndex = 0;
        List allOccurences = new ArrayList();
        while(fullPdfText.indexOf(substring,pointIndex) >= 0){
           allOccurences.add(fullPdfText.indexOf(substring, pointIndex));
           pointIndex = fullPdfText.indexOf(substring, pointIndex) + substring.length();
        }
        return allOccurences;
    }
    

提交回复
热议问题