Java String Substring Count countSubString(String strToFind, String strSearch)

Here you can find the source of countSubString(String strToFind, String strSearch)

Description

count Sub String

License

Open Source License

Declaration

public static int countSubString(String strToFind, String strSearch) 

Method Source Code

//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;

        }

    }
}

Related

  1. countSubString(final String aString, final String aSubString)
  2. countSubstring(final String text, final String substring)
  3. countSubString(String str, String substr)
  4. countSubstring(String string, String substring)
  5. countSubstringOccurrances(String s1, String SubString)
  6. countSubstrings(String string, String sub)