Here you can find the source of countSubString(String strToFind, String strSearch)
public static int countSubString(String strToFind, String strSearch)
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 SirLordT <sirlordt@gmail.com>. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v2.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * /*from w ww . j a va2 s. com*/ * Contributors: * SirLordT <sirlordt@gmail.com> - initial API and implementation ******************************************************************************/ public class Main { public static int countSubString(String strToFind, String strSearch) { try { int intCount = 0; for (int intFromIndex = 0; intFromIndex > -1; intCount++) { intFromIndex = strToFind.indexOf(strSearch, intFromIndex + ((intCount > 0) ? 1 : 0)); } return intCount - 1; } catch (Exception Ex) { return -1; } } }