Here you can find the source of countMatches(final CharSequence str, final CharSequence sub)
public static int countMatches(final CharSequence str, final CharSequence sub)
//package com.java2s; /**/*from w ww . j a va 2s . c om*/ * Copyright (c) 2015-2016 Angelo ZERR. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation */ public class Main { /** * Represents a failed index search. */ public static final int INDEX_NOT_FOUND = -1; private static final int NOT_FOUND = -1; public static int countMatches(final CharSequence str, final CharSequence sub) { if (isEmpty(str) || isEmpty(sub)) { return 0; } int count = 0; int idx = 0; while ((idx = indexOf(str, sub, idx)) != INDEX_NOT_FOUND) { count++; idx += sub.length(); } return count; } public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } static int indexOf(final CharSequence cs, final CharSequence searchChar, final int start) { return cs.toString().indexOf(searchChar.toString(), start); } static int indexOf(final CharSequence cs, final int searchChar, int start) { if (cs instanceof String) { return ((String) cs).indexOf(searchChar, start); } final int sz = cs.length(); if (start < 0) { start = 0; } for (int i = start; i < sz; i++) { if (cs.charAt(i) == searchChar) { return i; } } return NOT_FOUND; } }