How to find how many times does a string object repeat in java?

后端 未结 7 1506
南旧
南旧 2021-01-21 18:45

I have to String objects:

String first = \"/Some object that has a loop in it object/\";
String second = \"object\";

What I need to do is find

7条回答
  •  无人共我
    2021-01-21 19:30

    Use this :

     String first = "/Some object that has a loop in it object/";
         String second = "object";
         Pattern pattern = Pattern.compile(second);
         Matcher matcher = pattern.matcher(first) ;
         long count = 0;
         while(matcher.find()) {
             count ++;
    
         }
         System.out.println(count);
    

提交回复
热议问题