Here you can find the source of countMatches(String str, String sub)
private static int countMatches(String str, String sub)
//package com.java2s; public class Main { private static int countMatches(String str, String sub) { if (str == null || str.length() == 0 || sub == null || sub.length() == 0) { return 0; }//ww w.j av a 2 s . c om int count = 0; int idx = 0; while ((idx = str.indexOf(sub, idx)) != -1) { count++; idx += sub.length(); } return count; } }