Here you can find the source of countMatches(String str, String sub)
public static int countMatches(String str, String sub)
//package com.java2s; //License from project: LGPL public class Main { public static int countMatches(String str, String sub) { if (isEmpty(str) || isEmpty(sub)) { return 0; }//www. j a v a2 s . c o m int count = 0; int idx = 0; while ((idx = str.indexOf(sub, idx)) != -1) { count++; idx += sub.length(); } return count; } public static boolean isEmpty(String text) { return text == null || text.trim().length() == 0; } }