Java Find word in a String

后端 未结 5 1315
清酒与你
清酒与你 2021-01-15 10:26

I need to find a word in a HTML source code. Also I need to count occurrence. I am trying to use regular expression. But it says 0 match found.

I am using regular ex

5条回答
  •  庸人自扰
    2021-01-15 11:18

    To find a string in Java you can use String methods indexOf which tells you the index of the first character of the string you searched for. To find all of them and count them you can do this (there might be a faster way but this should work). I would recommend using StringUtils CountMatches method.

    String temp = string; //Copy to save the string
    int count = 0;
    String a = "hsw.ads";
    int i = 0;
    
    while(temp.indexOf(a, i) != -1) {
        count++;
        i = temp.indexof(a, i) + a.length() + 1;
    }
    

提交回复
热议问题