1: 2: 3: 4: 5: 6: 7: 8: 9:
String str = "ThisIsATestThisIsOnlyATest"; String word = "Test"; int count = 0; int idx = 0; while ((idx = str.indexOf(word, idx)) != -1) { count++; idx += word.length(); } return count;