Java String Substring Count countSubString(final String aString, final String aSubString)

Here you can find the source of countSubString(final String aString, final String aSubString)

Description

count Sub String

License

Open Source License

Parameter

Parameter Description
aString a parameter
aSubString a parameter

Return

the number of the searched sub-string int the string

Declaration

public static int countSubString(final String aString,
        final String aSubString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * 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://w w w. j  a  va 2s.c o m
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * @param aString
     * @param aSubString
     * @return the number of the searched sub-string int the string
     */
    public static int countSubString(final String aString,
            final String aSubString) {

        if (aString == null || aSubString == null || aString.isEmpty()
                || aString.length() < aSubString.length()) {
            return -1;
        }
        String[] wSplited = aString.split(aSubString);

        return wSplited.length - 1;
    }
}

Related

  1. countSubstring(final String text, final String substring)
  2. countSubString(String str, String substr)
  3. countSubstring(String string, String substring)
  4. countSubString(String strToFind, String strSearch)