indexOf to find all occurrences of a word in a String

别说谁变了你拦得住时间么 提交于 2019-12-05 21:54:55

You can keep track of the index:

int index = theString.indexOf("the");
while(index >= 0) {
    index = theString.indexOf("the", index+1);
    counter2++;
}

You have taken a complicated approach. Try this:

int count = str.split("the").length - 1;

If you absolutely must use indexOf():

str = str.toLowerCase();
int count = 0;
for (int i = str.indexOf("the"); i >= 0; i = str.indexOf("the", i + 1))
    count++;

indexOf can take a second argument that says where to start in the string. So after you find the first occurrence, you can tell indexOf to only search the string after that index, etc.

This code should work:

public static void main(String[] args) {
    String theString = "The other day I went over there.";
    theString = theString.toLowerCase();

    int index = -1;
    int count = 0;

    while (true) {
        index = theString.indexOf("the", index + 1);
        if (index == -1) {
            break;
        } else {
            count += 1;
        }
    }

    System.out.printf("The string 'the' was found %d times.\n", count);

    // Output:
    // The string 'the' was found 3 times.
}

The indexOf(String str) method finds the index of the first occurrence of a str. So if you want to find all the occurrances of str then I'd suggest just implementing a loop in which you cut the string once you find an instance of str and look for str within theString again until it is no longer in theString. Here is what it should look like,

int index = theString.indexOf("the")
int count = 0;
while(index >= 0 && theString.length() > 0){
     count++;
     theString = theString.substring(0, index + "the".length());
     index = theString.indexOf("the");
}

If you want to specifically use indexOf (I presume this is for an assignment), you can use a recursive method.

public static String numMatches (String str, String query) {
    int index = str.indexOf(query);
    if (index == -1) 
        return 0;
    else
        return 1 + numMatches(str.substring(index + query.length), query);
}

Maybe you can do something like this, you can use indexOf(String str)

String word = "We are students of the Department of Informatics. All students should know how to program in Java and C.";

    int findit = word.indexOf("students");
    int count = 0;

    for(int i=0; i<=findit; i++)
    {

        findit = word.indexOf("students" , findit + 1);

        count = count + 1;

    }

    System.out.print(count);

This checks how many occurrences of a string is inside a string.

             String str = oIn.nextLine();
             String st = oIn.nextLine();
             int countS = 0;
             int index3 = str3.indexOf(st);
             while (index >= 0){
             index = str.indexOf(st, index+1);
             countS++;          
             }
             System.out.println("The number of occurrences of "+st +" in the string " +str3 +" are "+countS);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!