public static void main(String[] args) { String str1 = "hello from lagos"; String guess = "l"; int index = str1.indexOf(guess); System.out.println("index is : " + index); // to show where the first occurrence is for(int i = 0; i < str1.length(); i++) { System.out.println("For many ocuurrences: " + index); index = str1.indexOf(guess, index + 1); } }
index is : 2 For many ocuurrences: 2 For many ocuurrences: 3 For many ocuurrences: 11 For many ocuurrences: -1 For many ocuurrences: 2 For many ocuurrences: 3 For many ocuurrences: 11 For many ocuurrences: -1 For many ocuurrences: 2 For many ocuurrences: 3 For many ocuurrences: 11 For many ocuurrences: -1 For many ocuurrences: 2 For many ocuurrences: 3 For many ocuurrences: 11 For many ocuurrences: -1
do
while
-1
for
indexOf
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)