2013-09-26 18 views

cevap

84

Kullanımı aşırı versiyonu:

str.indexOf("is", str.indexOf("is") + 1); 
19
int first = string.indexOf("is"); 
int second = string.indexOf("is", first + 1); 

Bu aşırı yükleme, belirtilen dizinden alt dizeyi aramaya başlar. 2 parametre olarak başlayan Indes alır indexOf() ait

+0

ne olur oluş iki kattan fazla mı? –

+1

Sonra özel bir şey olmaz, yine de ikinci olayı alır. –

+0

Üçüncü meydana gelme indeksi ne olacak? –

0

i bir döngü kullanılabilir düşünüyorum.

1 - check if the last index of substring is not the end of the main string. 
2 - take a new substring from the last index of the substring to the last index of the main string and check if it contains the search string 
3 - repeat the steps in a loop 
Sen oluşum pozisyonların dizisi dönmek için bir işlev yazabilirsiniz
0

Java

public static ArrayList<Integer> occurrencesPos(String str, String substr) { 
    final boolean ignoreCase = true; 
    int substrLength = substr.length(); 
    int strLength = str.length(); 

    ArrayList<Integer> occurrenceArr = new ArrayList<Integer>(); 

    for(int i = 0; i < strLength - substrLength + 1; i++) { 
     if(str.regionMatches(ignoreCase, i, substr, 0, substrLength)) { 
      occurrenceArr.add(i); 
     } 
    } 
    return occurrenceArr; 
} 
İlgili konular