How to find the count of substring in java

前端 未结 4 1718
走了就别回头了
走了就别回头了 2021-01-01 02:43

I am trying to find the count of the substring in a big string of length 10000 characters. Finally I need to remove all the substring in it. example s = abacacac, subs

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 03:15

    What about:

    String temp = s.replace(sub, "");
    int occ = (s.length() - temp.length()) / sub.length();
    

    Just remove all the substring, then check the difference on string length before and after removal. Divide the temp string with number of characters from the substring gives you the occurrences.

提交回复
热议问题