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: Apache License public class Main { public static int countMatches(String str, String sub) { if ((isEmpty(str)) || (isEmpty(sub))) { return 0; }/* w w w. j a v a2 s. c om*/ int count = 0; int idx = 0; while ((idx = str.indexOf(sub, idx)) != -1) { count++; idx += sub.length(); } return count; } public static boolean isEmpty(String str) { return (str == null) || (str.length() == 0); } public static int indexOf(String str, char searchChar) { if (isEmpty(str)) { return -1; } return str.indexOf(searchChar); } public static int indexOf(String str, char searchChar, int startPos) { if (isEmpty(str)) { return -1; } return str.indexOf(searchChar, startPos); } public static int indexOf(String str, String searchStr) { if ((str == null) || (searchStr == null)) { return -1; } return str.indexOf(searchStr); } public static int indexOf(String str, String searchStr, int startPos) { if ((str == null) || (searchStr == null)) { return -1; } if ((searchStr.length() == 0) && (startPos >= str.length())) { return str.length(); } return str.indexOf(searchStr, startPos); } }