code: public static int countMatches(String str, String sub) {
        if (isEmpty(str) || isEmpty(sub)) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(sub, idx)) != -1) {
            count++;
            idx += sub.length();
        }
        return count;
    }
docs: <p>Counts how many times the substring appears in the larger String.</p>
  
  <p>A <code>null</code> or empty ("") String input returns <code>0</code>.</p>
  
  <pre>
  GosuStringUtil.countMatches(null, *)       = 0
  GosuStringUtil.countMatches("", *)         = 0
  GosuStringUtil.countMatches("abba", null)  = 0
  GosuStringUtil.countMatches("abba", "")    = 0
  GosuStringUtil.countMatches("abba", "a")   = 2
  GosuStringUtil.countMatches("abba", "ab")  = 1
  GosuStringUtil.countMatches("abba", "xxx") = 0
  </pre>
  
  @param str  the String to check, may be null
  @param sub  the substring to count, may be null
  @return the number of occurrences, 0 if either String is <code>null</code>