fork download
  1. import java.util.regex.MatchResult;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Main {
  5. public static void main(String args[]) {
  6. // find the occurance of 'CO' in the given string using stream API
  7. String str = "WELCOMEWELCOME";
  8. String substring = "CO";
  9.  
  10. System.out.println(getSubstringCount(str, substring));
  11. }
  12. static long getSubstringCount(String str, String substring) {
  13. return Pattern.compile(substring)
  14. .matcher(str)
  15. .results()
  16. .map(MatchResult::group)
  17. .count();
  18. }
  19. }
Success #stdin #stdout 0.07s 48608KB
stdin
Standard input is empty
stdout
2